Hudzilla.org - the homepage of Paul Hudson
Contents > Objects Wish List | Report Bug | About Me ]

6.3     Objects

This is NOT the latest copy of this book; click here for the latest version.

Classes are mere definitions - you cannot play fetch with the definition of a dog, you need a real, live, slobbering dog. Naturally we cannot create live animals in our PHP scripts, but we can do the next best thing - creating an instance of our class.

In our earlier example, "Poppy" was a dog of type "poodle". We can create Poppy by using the following syntax:

$poppy = new poodle;

That creates an instance of the class poodle, and places it into the variable $poppy. Poppy, being a dog, can bark by using the bark() function, and to do this you need to use the special -> dereference marker. Here is a complete script demonstrating creating objects - note that the function override for bark() is commented out.

<?php
    
class dog {
        public function
bark() {
            print
"Woof!\n";
        }
    }

    class
poodle extends dog {
        
/* public function bark() {
            print "Yip!\n";
        } */
    
}

    
$poppy = new poodle;
    
$poppy->bark();
?>

Execute that script, and you should get "Woof!". Now try taking out the comments around the bark() function in the poodle class, and running it again you should see "Yip!" instead.





<< 6.2.4 Overriding functions   6.4 Variables >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
SID TRIVEDI - 29 Aug 2008

<?php
/**
* TITLE : DIFFERENCE BETWEEN PHP OBJECT ORIENTED PROGRAMMING & PHP PROCEDURAL PROGRAMMING.
* SCOPE : To explain / understand PHP OOPs in an easier way.
* Credit : Practical PHP Programming by PAUL HUDSON
* This codes has been tested OK on PHP ver 5.2.4
*/

class dog {
function bark() {
print "Grrrrrrr...Woof...Woof!\n<br>";
}
}
class poodle extends dog {
function bark() {
print "Yip...Yeep...Yeeeeeeeeeeeeep!\n<br>";
}
}
$poppy = new dog;
$poppy->Name = "Poppy";
print $poppy->Name;
echo "\n<br>";
$poppy->bark(); // function call bark() with class as dog
$penny = new poodle;
$penny->Name = "Penny";
print $penny->Name;
echo "\n<br>";
$penny->bark(); // function call bark() with extended class as poodle

/*
$german_sheppard = new dog;
$german_sheppard->Name = "German Sheppard";
print $german_sheppard->Name;
echo "\n<br>";
$german_sheppard->bark(); // function call bark() with class as dog
*/

/* Above OOP - Object Oriented Programming approach (codes) produces following (same) browser output:
Poppy
Grrrrrrr...Woof...Woof!
Penny
Yip...Yeep...Yeeeeeeeeeeeeep!

The difference between procedural programming and an OOP aproach is ease of manipluating around the objects and its class. Lets say we wanna add German Sheppard to dog class, its easy....
*/

// Now producing same browser output with Procedural Programming Apprach:

/*
$dog = null; // Initial definition and value assignment of variable $dog
$poppy = 'Poppy';
$penny = 'Penny';
//$german_sheppard = 'German Sheppard';
function bark($dog){
if ($dog === 'Poppy'){
print "$dog\n<br>";
print "Grrrrrrr...Woof...Woof!\n<br>";
return;
}
if($dog === 'Penny'){
print "$dog\n<br>";
print "Yip...Yeep...Yeeeeeeeeeeeeep!\n<br>";
return;
}

// if ($dog === 'German Sheppard'){
// print "$dog\n<br>";
// print "Grrrrrrr...Woof...Woof!\n<br>";
// return;
// }

else{
return;
}
}
bark ($poppy); //calling function bark on variable $poppy-->$do

A New PHP User - 29 Aug 2008

I have the same problem. This is probably so because I have PHP 4.3.2 installed which doesn't have all the functionality (concerning classes)of PHP 5

cmani@yahoo.com - 29 Aug 2008

When I tried the above code, it gave me foll error. Can somebody explain why ?

Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in c:\program files\apache group\apache\htdocs\cmani\class1.php on line 3

When I dropped "public" it did not give me an error.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is seven plus ten?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow