PHP programming - Classes and Objects
Frank Yap

This section explains more about class objects.
 
Resources

 
Lab Exercises
Exercise 1: $this pseudo-variable

That $this pseudo-variable.
In the following code observe when $this is defined and when it is not.

class classA 

function foo() 

if (isset($this)) { 
echo '$this is defined - ('; 
echo get_class($this); 
echo ")<br>"; 
} else { 
echo "$this is not defined.<br>"; 



 
class classB 

function bar() 

// Note: the next line will issue a warning if E_STRICT is enabled. 
classA::foo(); 


 
// Temporarily disable E_STRICT warning. 
$reportLevel = error_reporting(); 
$newLevel = $reportLevel & (~E_STRICT); 
error_reporting($newLevel); 
 
 
$a = new classA(); // $this gets defined here 
$a->foo(); // $this is defined 
 
// Note: the next line will issue a warning if E_STRICT is enabled. 
classA::foo(); // $this is not defined 
 
$b = new classB(); // $this gets defined here 
$b->bar(); // $this is defined 
 
// Note: the next line will issue a warning if E_STRICT is enabled. 
classB::bar(); // $this is not defined 
 
// Restore E_STRICT warning. 
error_reporting($reportLevel);

Output ---

$this is defined - (classA)
$this is not defined.
$this is defined - (classB)
$this is not defined.
 
Exercise 2: Creating new objects

Creating new objects. Study each case.
 
class Test 

}
$obj1 = new Test(); 
$obj2 = new $obj1;
$obj1 === $obj2 ?     bool(false)
$obj1 == $obj2 ?     bool(true)
var_dump($obj1)     object(Test)#3 (0) { }
var_dump($obj2)     object(Test)#4 (0) { }

$obj4 =& $obj1;
$obj4 === $obj1 ?     bool(true)
 
Exercise 3: New self and new static

Difference between new self and new static.
new self - refers to the class in which the new operation takes place in.
new static - late static bindings refers to the class from which you call the method.
 
class E { 
public static function get_E () { 
return new self(); 
}  
public static function get_me () { 
return new static(); 

}  
class F extends E {}  
echo get_class(F::get_E())."<br>"; // E  
echo get_class(F::get_me())."<br>"; // F  
echo get_class(E::get_me())."<br>"; // E
 
Exercise 4: Autoloading classes

Autoloading classes
Autoloading function gets executed behind from visible code to include necessary files.

 
function __autoload ($class){ 
echo "included " . $class . " --<br>"; 
include $class.'.php'; // generate proper path and file spec 

 
 
$obj=new Dog (); // executes __autoload to load Dog.php 
$obj->speak (); 
$obj2=new Cat (); // executes __autoload to load Cat.php 
$obj2->speak (); 
 
class Foo extends Dog { // executes __autoload to load Dog.php 

 
$obj3 = new Foo; // executes __autoload to load Dog.php 
$obj3->speak(); 
 
// 
class Template implements iTemplate{ // executes __autoload to load iTemplate.php 
private $vars=array(); 
 
public function setVariable($name,$var){ 
$this->vars[$name]=$var; 

public function getHtml($template){ 
foreach ($this->vars as $name=>$value){ 
$template=str_replace('{'.$name.'}',$value,$template); 

return$template; 

}
included Dog --
O:3:"Dog":0:{} -- I am a dog.
included Cat --
O:3:"Cat":0:{} -- I am a cat.
O:3:"Foo":0:{} -- I am a dog.
included iTemplate --
 
Autoloading with spl_autoload_register() function.
With spl_autoload_register(), more than one autoload function could be installed to load files from different directories.
Study where each autoload function searches.
 
function myAutoload ($class){ 
echo "<br>search " . serialize ($class); 
 
$filename = $class . '.php'; 
$file = $filename; 
if (! file_exists($file)) 
return false; 
include $file; 

 
function myAutoload_1 ($class){ 
echo "<br>search " . serialize ($class); 
 
$filename = $class . '.php'; 
$file ='inc_1/' . $filename; 
if (! file_exists($file)) 
return false; 
include $file; 

 
function myAutoload_2 ($class){ 
echo "<br>search " . serialize ($class); 
 
$filename = $class . '.php'; 
$file ='inc_2/' . $filename; 
if (! file_exists($file)) 
return false; 
include $file; 

 
 
spl_autoload_register ('myAutoload'); 
spl_autoload_register ('myAutoload_1'); 
spl_autoload_register ('myAutoload_2'); 
 
 
$obj=new Tiger (); // searches registered loaders to load Tiger.php 
$obj->speak (); 
$obj2=new Lion (); // searches registered loaders to load Lion.php 
$obj2->speak (); 
 
class IndianTiger extends Tiger { // searches registered loaders to load Tiger.php 

 
$obj3 = new IndianTiger; 
$obj3->speak();

search s:5:"Tiger";
search s:5:"Tiger"; O:5:"Tiger":0:{} -- I am a tiger.

search s:4:"Lion";
search s:4:"Lion";
search s:4:"Lion";O:4:"Lion":0:{} -- I am a lion.
O:11:"IndianTiger":0:{} -- I am a tiger.
 

Homework Exercise

Practice using spl_autoload_register() function.
 
Start XAMPP Apache server.
 
1) Create a couple of class folders in your project folder.
2) Store several class files in those created class folders.
3) Create a project file and add codes that instantiates objects from the classes stored in the class folders.
4) Add the spl_autoload_register() functions in the project code, so that necessary files get included.
5) Verify that the code works.
 
Submit the codes:
Submit the codes for the classes.
Submit the project code.