Programming in PHP utilizing objects is not complex and the syntax and other basics are well covered in various articles and tutorials. It’s also covered in the PHP manual and I will include a brief example for those who need a refresher. Before that I am going to illustrate the basic principals of references without using objects at all. I will then build upon that example to show how use objects and references in your program in useful and efficient ways.

The first thing I will show is just what a reference is. A reference is a variable that is connected to the contents of another variable. Changing one variable will change both.

$a = 5;

$b = &$a; //the and symbol means your are assigning a reference.

echo $b . "<br />"; //you will see 5

$a = 101;

echo $b . "<Br />"; //you will see 101

if you had used $a = $b; earlier instead of $b = &$a; you would see 5, but because you made b a reference the contents of the 2 will always be the same.

There are other important aspects of references, like how they relate to functions.

function square_it(&$a){

	$a = $a * $a;

}

$oranges = 5;

square_it($oranges);

echo $oranges; //you will see 25

The principal is the same, just with a function parameter. $a = &$oranges; is probably what we would expect, but the syntax is as shown above. Without the & symbol we are only copying contents from variable one to the other, and this applies to objects as well. One use for this is actually to get a function to have more return values/variables. Even some built in PHP functions use pass by reference variables because they need to “return” several values. Calling a function and using the & symbol there is usually not allowed, for example you wouldn’t use $grapes = myfunction(&$oranges);.

Turning back to the subject of objects, here is the basic way a class is defined and an object is created.

class MyCart{
	var $items;

	function add_item ($artnr, $num){

		$this->items[$artnr] += $num;

	}

	function remove_item ($artnr, $num){

		if ($this->items[$artnr] > $num) {

			$this->items[$artnr] -= $num;

			return true;

		} else {

			return false;

		}

	}

}

$usercart = new MyCart();

We have defined one class and created an ‘instance’ of it, an object of type MyCart. One advantage of what we just did is we now have multiple functions and variables available inside the object and so far we only declared one global variable. I will expand on that point at the end of the article, for now let’s make 2 new classes.

class MySystem{

	var $system;

	var $html_page;

	__construct() {

		$this->system = new MySearch();

		$this->html_page = "some html";

	}

}

class MySearch{

	__construct() {

	}

}

$thesystem = new MySystem();

Notice the construct functions. These are standard php functions, they are called when the new class command is used, for example new MySystem();. In that case, look at the construct function, it creates a MySearch instance. So we have 2 objects, but one is internal to thesystem. We saved ourselves the trouble of making a new MySearch instance separately and we also did not use a global variable for it.

Let’s assume that Mysearch needs some functions and one of them needs to read the variable $html_page that is inside the MySystem instance. We could pass the MySystem instance as a parameter in the function. Probably it would be a parameter that is a reference, like the example earlier called square_it(). That would be an acceptable solution, but the programmer might also choose to hold the creating class in a permanent reference.

class MySystem{

	var $system;

	var $html_page;

	__construct() {

		$this->system = new MySearch($this); //we are passing the $this variable, which means the object it's self

		$this->html_page = "some html";

	}

}

class MySearch{

	var $par

	__construct(&$setparent) {
//we are intending to recieve the $this variable shown above, and since we have the & symbol, it is a reference

		$this->par = &$setparent; //don't forget the & symbol, because again we want a reference not a copy

	}

	function searchparent(){

		return search("?", $this->par->html_page);

	}

}

$thesystem = new MySystem();

We created 2 classes, and once constructed the instances each have a variable to access the other. The syntax is simple enough, for example $this->par->html_page, which simply means the variable html_page from the parent that was set in __construct. In the context of an article the simplified classes seem strange and unnecessary, but what I am illustrating here is a system of organization that is very widespread in object oriented programming. The reasons to use it are many, such as ease of use, efficiency, and preventing bugs and conflicts. Note that the functions and member variables inside the classes are all public in these examples by default. In practice programmers only give the classes (and global scope) public access to functions and variables when necessary. There are also other types like friend classes but those details won’t be covered here.

The main point I am trying to get across is how easy it is to create your whole program simply by creating one object, and all while keeping it contained instead of creating things in the global scope. Plugins and program independent code can be made in this manner with PHP, just as they are in many other programming languages.