Best Practice: access PHP variables from html

Hi

I am trying to teach myself PHP best practice. As a C++ programmer I hate global variables so I am planning to implement this in my next project. Previously, I have declared global PHP variables and then accessed them directly from HTML. But as already stated, global’s are bad news.

So this time I am planning implementing my next project as follows:

test.php

class TestClass
{
        // Function initialises the entire procedure
        public function startTest(){
            $test = array("Volvo","BMW","Toyota");
            return $test;
        }
}

html_output.php

<!DOCTYPE html>
<html>
    <head>
         <?php 
            include ('test.php'); 
            $forecast = new TestClass();
            $dataArray = $forecast->startTest();
         ?>
    </head>
    <body>
        <p><?php echo $dataArray[0] ?></p>
        <p><?php echo $dataArray[1] ?></p>
        <p><?php echo $dataArray[2] ?></p>
    </body>
</html>

Is this better than using global’s? Or. is there a better way of achieving this?

Many Thanks

I personally don’t make anything global unless it needs to be.

There is nothing wrong with using an Associative Array over an Indexed Array either. PHP Internally stores all arrays as associative arrays.[php]$dataArray[‘Volvo’][/php] It is exactly the same as $dataArray[0] and is much more readable. Even more readable would be [php]$carsArray[‘Volvo’][/php].

If someone else works on that code, they would have to open up another file and find reference to see what it is, or even yourself coming back to your own code a year later. Otherwise you would have to dump the variable on the page your on to see what it is rather than it just being obvious by the name.

As for global variables being hated, I would use function that auto loads your classes.

For example I have a file called utilities.inc.php that has this as part of the script or something similar:
[php]// Autoload classes from “classes” directory:
function class_loader($class) {
require(‘lib/classes/’ . $class . ‘.php’);
}[/php]

that way you can just call the file once in you main php file : [php]require(‘lib/includes/utilities.inc.php’);[/php]

that way all you need to do something like the following:
[php]<?php

require(‘lib/includes/utilities.inc.php’);

$forecast = new TestClass();
[/php]
this way you don’t have to remember load in the class in every time you want to use a certain class and you can also do interesting stuff in your utility file like:
[php]session_start();

// Check for a user in the session:
$user = (isset($_SESSION[‘user’])) ? $_SESSION[‘user’] : NULL;[/php]

I would also study up on setters, getters and constructors, a silly constructor I put together:
[php]<?php
class Cars {

protected $automobiles = array("Volvo", "BMS", "Toyota");

public function __construct(array $cars=NULL) {
	$this->automobiles;
}

}

$car = new Cars();

echo ‘

’;
print_r($car);
echo ‘
’;[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service