PHP 101 (part 7): The Bear Necessities

November 30, -0001

Tutorials

Alphabet Soup
Back To Class
Animal Antics
Going Deeper
This And That

Under Construction
Hands Off
Extending Yourself
Part 2



Alphabet Soup

So now you know how to create your own functions in PHP, and
you’ve spent the last few days busily inspecting your applications and
turning repeated code fragments into functions. But functions are just
the tip of the software abstraction iceberg. Lurking underneath is a
three-letter acronym that strikes fear into the hearts of most newbie
programmers.

OOP.

If you’ve been programming for a while, you’ve probably heard the
term OOP before – it stands for Object Oriented Programming, and refers
to a technique whereby you create program “objects”
and then use these objects to build the functionality you need into
your program. PHP 5 is very big on OOP – it comes with a
brand-spanking-new object model which finally brings PHP objects into
conformance with standard OOP principles and offers OO programmers a
whole bunch of new goodies to play with.

Wondering how you can get in on this? Well, wonder no more. Your
prayers have been answered.

Over the course of this tutorial, I’m going to take a
brief look at PHP’s OO capabilities (both PHP 4 and PHP 5), together
with examples and explanations to demonstrate just how powerful it
really is. I’ll be covering most of the basics – classes, objects,
attributes and methods – and a couple of more advanced concepts -
constructors, destructors, private methods and properties, and
inheritance. And if you’re new to object-oriented programming, or just
apprehensive about what lies ahead, don’t worry – I promise this will
be a lot less painful than you think. And unlike dentists, I don’t lie.


Back To Class

Before beginning, though, let’s make sure that you have a clear idea
of the concepts involved here.

In PHP, a class is simply a set of program statements which
perform a specific task. A typical class definition contains both
variables and functions, and serves as the template from which to spawn
specific instances of that class.

These specific instances of a class are referred to as
objects. Every object has certain characteristics, or
properties, and certain pre-defined functions, or
methods. These properties and methods of the object correspond
directly with the variables and functions within the class
definition.

Once a class has been defined, PHP allows you to spawn as many
instances of the class as you like. Each of these instances is a
completely independent object, with its own properties and methods, and
can therefore be manipulated independently of other objects. This comes in
handy in situations where you need to spawn more than one instance of
an object – for example, two simultaneous database links for two
simultaneous queries, or two shopping carts.

Classes also help you keep your code modular – you can define a
class in a separate file, and include that file only in the scripts
where you plan to use the class – and simplify code changes, since you
only need to edit a single file to add new functionality to all your
spawned objects.


Animal Antics

To understand this better, pick an animal, any animal. I pick the
bear, because I like bears. Now ask yourself, can you consider this
bear, within the framework of OOP, as an “object”?

Why not? After all, every bear has certain characteristics – age,
weight, sex – which are equivalent to object properties. And every bear
can perform certain activities – eat, sleep, walk, run, mate – all of
which are equivalent to object methods.

Let’s take it a little further. Since all bears share certain
characteristics, it is possible to conceive of a template Bear(), which
defines the basic characteristics and abilities of every bear on the
planet. Once this Bear() (“class”) is used to create a new

$bear (“object”), the individual characteristics of the newly-created
Bear can be manipulated independently of other Bears that may be created
from the template.

Now, if you sat down to code this class in PHP 5,
it would probably look something like this:


<?php

// PHP 5

// class definition

class Bear {

    
// define properties

    public $name;

    public
$weight;

    public
$age;

    public
$sex;

    public
$colour;

    // define methods

    public function eat() {

        echo
$this->name." is eating...
"
;

    }

    public function run() {

        echo
$this->name." is running...
"
;

    }

    public function kill() {

        echo
$this->name." is killing prey...
"
;

    }

    public function sleep() {

        echo $this->name." is sleeping...
"
;

    }

}

?>

Given this class, it’s now simple to spawn as many Bears as you
like, and adjust the individual properties of each. Take a look:


<?php

// my first bear

$daddy = new Bear;

// give him a name

$daddy->name = "Daddy Bear";

// how old is he

$daddy->age = 8;

// what sex is he

$daddy->sex = "male";

// what colour is his coat

$daddy->colour = "black";

// how much does he weigh

$daddy->weight = 300;

// give daddy a wife

$mommy = new Bear;

$mommy->name = "Mommy Bear";

$mommy->age = 7;

$mommy->sex = "female";

$mommy->colour = "black";

$mommy->weight = 310;

// and a baby to complete the family

$baby = new Bear;

$baby->name = "Baby Bear";

$baby->age = 1;

$baby->sex = "male";

$baby->colour = "black";

$baby->weight = 180;

// a nice evening in the Bear family

// daddy kills prey and brings it home

$daddy->kill();

// mommy eats it

$mommy->eat();

// and so does baby

$baby->eat();

// mommy sleeps

$mommy->sleep();

// and so does daddy

$daddy->sleep();

// baby eats some more

$baby->eat();

?>

As the illustration above shows, once new objects are defined, their
individual methods and variables can be accessed and modified
independent of each other. This comes in very handy, as the rest of this
tutorial will show.


Going Deeper

Now that you’ve got the concepts straight, let’s take a look at the
nitty-gritty of a class definition.


<?php

// PHP 5

// class definition

class Bear {

    // define public properties

    
public $name;

    public
$age;

    // more properties

    // define public methods

    public function eat() {

        echo
$this->name." is eating...
"
;

        
// more code

    
}

    // more methods

}

?>

Every class definition begins with the keyword class, followed by a
class name. You can give your class any name that strikes your fancy,
so long as it doesn’t collide with a reserved PHP word. A pair of curly
braces encloses all class variables and functions, which are written as
you would normally code them.

PHP
5 also introduces the concept of visibility to the object model.
Visibility controls the extent to which object properties and methods
can be manipulated by the caller, and plays an important role in
defining how open or closed your class is. Three levels of visibility
exist, ranging from most visible to least visible: public, private

and protected. Within the class definition, you can
mark the visibility of a property or method by preceding
it with one of the keywords – public,
private, or protected .

By default, class methods and properties are public; this allows the
calling script to reach inside your object instances and manipulate
them directly. If you don’t like the thought of this intrusion, you can
mark a particular property or method as private or protected,
depending on how much control you want to cede over the object’s internals (more
on this shortly).

Since the PHP 4 object model does not include
support for visibility, the class definition above would not work in
PHP 4. Instead, you would need to use the following:


<?php

// PHP 4

// class definition

class Bear {

    
// define properties

    
var $name;

    var $weight;

    var
$age;

    var
$sex;

    var
$colour;

    // define methods

    
function eat() {

        echo $this->name." is eating...
"
;

    }

    function run() {

        echo
$this->name." is running...
"
;

    }

    function kill() {

        echo
$this->name." is killing prey...
"
;

    }

    function sleep() {

        echo $this->name." is sleeping...
"
;

    }

}

?>

From the above, it should be clear that class properties and methods in PHP
4 are always public …and there ain’t nuttin’ you can do about that!

In order to create a new instance of a class, you use the new
keyword to assign the newly created object to a PHP variable.


<?php

$daddy = new Bear;

?>

In English, the above would mean “create a new object of class
Bear() and assign it to the variable $daddy “.

You can now access all the methods and properties of the class via
this variable. For example, the code


<?php

$daddy->name = "Daddy Bear";

?>

would mean “assign the value Daddy Bear to the variable $name of
this specific instance of the class Bear()“, while the statement


<?php

$daddy->sleep();

?>

would mean “execute the function sleep() for this specific instance
of the class Bear()“.

Note the -> symbol used to connect objects to their properties or
methods, and the fact that the $ symbol is omitted when accessing
properties of a class instance.


This And That

In case you need to access functions or variables within the class
definition itself, both PHP 4 and PHP 5 offer the $this
keyword, which is used to refer to “this” class. To see how this works,
let’s alter the eat() method to accept a number of food units and then
add that to the bear’s weight.


<?php

// PHP 5

// class definition

class Bear {

    
// define properties

    
public $name;

    public
$weight;

    // define methods

    
public function eat($units) {

        echo $this->name." is eating ".$units." units of food...
"
;

        
$this->weight += $units;

    }

}

?>

In this case, the $this prefix indicates that the variable to be
modified exists within the class – or, in English, “add the argument
provided to eat() to the variable $weight within this object”. The

$this prefix thus provides a convenient way to access variables and
functions which are “local” to the class.

Here’s an example of how it works:


<?php

// create instance

$baby = new Bear;

$baby->name = "Baby Bear";

$baby->weight = 1000;

// now create another instance

// this one has independent values for each property

$brother = new Bear;

$brother->name = "Brother Bear";

$brother->weight = 1000;

// retrieve properties

echo $baby->name." weighs ".$baby->weight." units
"
;

echo $brother->name." weighs ".$brother->weight." units
"
;

// call eat()

$baby->eat(100);

$baby->eat(50);

$brother->eat(11);

// retrieve new values

echo $baby->name." now weighs ".$baby->weight." units
"
;

echo $brother->name." now weighs ".$brother->weight." units
"
;

?>

The output of this will read:

Baby Bear weighs 1000 units
Brother Bear weighs 1000 units
Baby Bear is eating 100 units of food...
Baby Bear is eating 50 units of food...
Brother Bear is eating 11 units of food...
Baby Bear now weighs 1150 units
Brother Bear now weighs 1011 units


Under Construction

It’s also possible to automatically execute a function when the
class is called to create a new object. This is referred to in geek
lingo as a constructor and, in order to use it, your PHP 5
class definition must contain a special function, __construct().

For example, if you’d like all newly born bears to be brown and weigh
100 units, you could add this to your class definition:


<?php

// PHP 5

// class definition

class Bear {

    
// define properties

    
public $name;

    public $weight;

    public
$age;

    public
$colour;

    

    
// constructor

    
public function __construct() {

        
$this->age = 0;

        $this->weight = 100;

        
$this->colour = "brown";

    }

    

    
// define methods

}

?>

In PHP 4, your constructor must have the same name as the class.
Here’s the equivalent code for PHP 4:


<?php

// PHP 4

// class definition

class Bear {

    // define properties

    
var $name;

    var
$weight;

    var
$age;

    var
$colour;

    // constructor

    
function Bear() {

        $this->age = 0;

        
$this->weight = 100;

        
$this->colour = "brown";

    }

    

    // define methods

}

?>

Now, try creating and using an instance of the class:


<?php

// create instance

$baby = new Bear;

$baby->name = "Baby Bear";

echo
$baby->name." is ".$baby->colour." and weighs ".$baby->weight." units at birth";

?>

Here, the constructor automatically sets default properties every
time an object of the class is instantiated. Therefore, when you run
the script above, you will see this:

Baby Bear is brown and weighs 100 units at birth


Hands Off

As noted previously, PHP 5 makes it possible to mark class
properties and methods as private, which means that they cannot be
manipulated or viewed outside the class definition. This is useful to
protect the inner workings of your class from manipulation by object
instances. Consider the following example, which illustrates this by adding
a new private variable, $_lastUnitsConsumed,
to the Bear() class:


<?php

// PHP 5

// class definition

class Bear {

    
// define properties

    
public $name;

    public
$age;

    public $weight;

    private
$_lastUnitsConsumed;

    // constructor

    
public function __construct() {

        
$this->age = 0;

        $this->weight = 100;

        
$this->_lastUnitsConsumed = 0;

    }

    

    
// define methods

    
public function eat($units) {

        echo $this->name." is eating ".$units." units of food...
"
;

        
$this->weight += $units;

        $this->_lastUnitsConsumed = $units;

    }

    public function getLastMeal() {

        echo
"Units consumed in last meal were ".$this->_lastUnitsConsumed."
"
;

    }

}

?>

Now, since the $_lastUnitsConsumed variable is declared as private,
any attempt to modify it from an object instance will fail. Here is an
example:


<?php

$bob = new Bear;

$bob->name = "Bobby Bear";

$bob->eat(100);

$bob->eat(200);

echo
$bob->getLastMeal();

// the next line will generate a fatal error

$bob->_lastUnitsConsumed = 1000;

?>

In a similar way, class methods can also be marked as private - try
it out for yourself and see.


Extending Yourself

Two of the best things about OOP, whether in PHP 4 or in PHP 5, are
extensibility and inheritance. Very simply, this means that you can
create a new class based on an existing class, add new features (read:
properties and methods) to it, and then create objects based on this
new class. These objects will contain all the features of the original
parent class, together with the new features of the child class.

As an illustration, consider the following PolarBear() class, which
extends the Bear() class with a new method.


<?php

// PHP 5

// class definition

class Bear {

    
// define properties

    
public $name;

    public
$weight;

    public
$age;

    public
$sex;

    public $colour;

    // constructor

    
public function __construct() {

        
$this->age = 0;

        
$this->weight = 100;

    }

    // define methods

    
public function eat($units) {

        echo
$this->name." is eating ".$units." units of food...
"
;

        $this->weight += $units;

    }

    public function run() {

        echo
$this->name." is running...
"
;

    }

    public function kill() {

        echo
$this->name." is killing prey...
"
;

    }

    public function sleep() {

        echo $this->name." is sleeping...
"
;

    }

}

// extended class definition

class PolarBear extends Bear {

    // constructor

    
public function __construct() {

        
parent::__construct();

        
$this->colour = "white";

        
$this->weight = 600;

    }

    // define methods

    
public function swim() {

        echo
$this->name." is swimming...
"
;

    }

}

?>


The extends keyword is used to extend a parent class to a child
class. All the functions and variables of the parent class immediately
become available to the child class. This is clearly visible in the
following code snippet:


<?php

// create instance of Bear()

$tom = new Bear;

$tom->name = "Tommy Bear";

// create instance of PolarBear()

$bob = new PolarBear;

$bob->name = "Bobby Bear";

// $bob can use all the methods of Bear() and PolarBear()

$bob->run();

$bob->kill();

$bob->swim();

// $tom can use all the methods of Bear() but not PolarBear()

$tom->run();

$tom->kill();

$tom->swim();

?>

In this case, the final call to $tom->swim() will fail and cause
an error, because the Bear() class does not contain a swim() method.
However, none of the calls to $bob->run() or $bob->kill() will
fail, because as a child of the Bear() class, PolarBear() inherits all
the methods and properties of its parent.

Note how the parent class constructor has been called in the
PolarBear() child class constructor - it's a good idea to do this so
that all necessary initialization of the parent class is carried out when a
child class is instantiated. Child-specific initialization can then be
done in the child class constructor. Only if a child class does not
have a constructor, is the parent class constructor automatically called.

You can do this in PHP 4, too. Here's a PHP 4 version of the PolarBear
class definition:


<?php

// PHP 4

// extended class definition

class PolarBear extends Bear {

    // constructor

    
function PolarBear() {

        
parent::Bear();

        
$this->colour = "white";

        $this->weight = 600;

    }

    // define methods

    
function swim() {

        echo
$this->name." is swimming...
"
;

    }

}

?>

To prevent a class or its methods from being inherited, use the
final keyword before the class or method name (this is new in PHP 5
and will not work in older versions of PHP). Here's an example, which
renders the Bear() class un-inheritable (if that's actually a
word):


<?php

// PHP 5

// class definition

final class Bear {

    
// define properties

    // define methods

}

// extended class definition

// this will fail because Bear() cannot be extended

class PolarBear extends Bear {

        // define methods

}

// create instance of PolarBear()

// this will fail because Bear() could not be extended

$bob = new PolarBear;

$bob->name = "Bobby Bear";

echo
$bob->weight;

?>

PHP 101 (part 7): The Bear Necessities - Part 2


Copyright Melonfire, 2004 (http://www.melonfire.com).
All rights reserved.

,

25 Responses to “PHP 101 (part 7): The Bear Necessities”

  1. darrcoding Says:

    The name of the -> operator is a pointer. The same name is derived from C and C++ for an object referencing a property.

  2. radwived Says:

    if i remember correctly, in C++, it’s being called as ‘value at’ operator. Must be the same here as well.

    Thanks for such a great and interesting tutorial!

  3. adarshakb Says:

    a big thanks to the the person who created of this tutorial…. from my heart

  4. static12 Says:

    Thanks a bunch for the tutorial, was a great review for me about oop in php.

  5. piekkon Says:

    Here is what I am getting:

    name . " is eating…
    "; } public function run() { echo $this->name." is running…
    "; } public function kill() { echo $this->name." is killing prey…
    "; } public function sleep() { echo $this->name." is sleeping…
    "; } } ?> name = "Daddy Bear"; // how old is he $daddy->age = 8; // what sex is he $daddy->sex = "male"; // what colour is his coat $daddy->colour = "black"; // how much does he weigh $daddy->weight = 300; // give daddy a wife $mommy = new Bear; $mommy->name = "Mommy Bear"; $mommy->age = 7; $mommy->sex = "female"; $mommy->colour = "black"; $mommy->weight = 310; // and a baby to complete the family $baby = new Bear; $baby->name = "Baby Bear"; $baby->age = 1; $baby->sex = "male"; $baby->colour = "black"; $baby->weight = 180; // a nice evening in the Bear family // daddy kills prey and brings it home $daddy->kill(); // mommy eats it $mommy->eat(); // and so does baby $baby->eat(); // mommy sleeps $mommy->sleep(); // and so does daddy $daddy->sleep(); // baby eats some more $baby->eat(); ?>

    I have tried to fix the problem but don’t know what else to do. Here is my code:
    <head>
    <title>Bear</title>
    </head>

    <body>
    <?php
    // PHP 5

    // class definition
    class Bear {
    // define properties
    public $name;
    public $weight;
    public $age;
    public $sex;
    public $colour;

    // define methods
    public function eat() {
    echo $this->name . " is eating…<br />";
    }

    public function run() {
    echo $this->name." is running…<br />";

    }

    public function kill() {
    echo $this->name." is killing prey… <br />";

    }

    public function sleep() {
    echo $this->name." is sleeping… <br />";

    }
    }

    ?>

    <?php

    // my first bear
    $daddy = new Bear;
    // give him a name
    $daddy->name = "Daddy Bear";
    // how old is he
    $daddy->age = 8;
    // what sex is he
    $daddy->sex = "male";
    // what colour is his coat
    $daddy->colour = "black";
    // how much does he weigh
    $daddy->weight = 300;

    // give daddy a wife
    $mommy = new Bear;
    $mommy->name = "Mommy Bear";
    $mommy->age = 7;
    $mommy->sex = "female";
    $mommy->colour = "black";
    $mommy->weight = 310;

    // and a baby to complete the family
    $baby = new Bear;
    $baby->name = "Baby Bear";
    $baby->age = 1;
    $baby->sex = "male";
    $baby->colour = "black";
    $baby->weight = 180;

    // a nice evening in the Bear family
    // daddy kills prey and brings it home
    $daddy->kill();

    // mommy eats it
    $mommy->eat();
    // and so does baby
    $baby->eat();

    // mommy sleeps
    $mommy->sleep();
    // and so does daddy
    $daddy->sleep();

    // baby eats some more
    $baby->eat();

    ?>

    </body>

    </html>

    I don’t know what else to do or try. Can someone please help?

    I made sure my file extension was .php also.

  6. _____anonymous_____ Says:

    One word this is excellent

  7. _____anonymous_____ Says:

    Hey PHP gurus,

    What’s the name of this ‘->’ operator? Meaning how do you pronounce it?

  8. aliaspanther Says:

    This is in regards to phileasphogg’s latest comment:

    You do realize you made the Mommy Bear a male, and the Daddy a female, don’t you?
    Just found that kinda humorous :P

    Anyways, great set of tutorials. I’m really enjoying the refresher as well as learning new things.

  9. _____anonymous_____ Says:

    This is in answer to thattotteyguy

  10. saugatabose Says:

    here i have 2 files. i want to access Bear3.php from family3.php. i failed to do this. could u plz hell me to find out the problem?
    Bear3.php
    <?php
    class Bear3{
    public $name;
    public $age=0;
    public $weight=0;
    public $count=1;

    public function _construct(){

    $this->age=10;
    $this->weight=1;
    $this->count++;
    }

    }
    ?>
    family3.php
    <?php
    require_once(‘Bear3.php’);

    $daddy=new Bear3();

    $name=$daddy->name="DADDY";

    echo "<br>AGE= $daddy->age";
    ?>

  11. thattolleyguy Says:

    I woke up this morning and tried the same code and it works now. Thanks whoever fixed it! Thanks for the great tutorials as well!!

  12. thattolleyguy Says:

    I just had a problem I’ve run into whenever I try to access a property or function from a class. Using the above example from the "Animal Antics" section, I get the following outputted in my browser:

    name." is eating… "; } public function run() { echo $this->name." is running… "; } public function kill() { echo $this->name." is killing prey… "; } public function sleep() { echo $this->name." is sleeping… "; } } ?> name = "Daddy Bear"; // how old is he $daddy->age = 8; // what sex is he $daddy->sex = "male"; // what colour is his coat $daddy->colour = "black"; // how much does he weigh $daddy->weight = 300; // give daddy a wife $mommy = new Bear; $mommy->name = "Mommy Bear"; $mommy->age = 7; $mommy->sex = "female"; $mommy->colour = "black"; $mommy->weight = 310; // and a baby to complete the family $baby = new Bear; $baby->name = "Baby Bear"; $baby->age = 1; $baby->sex = "male"; $baby->colour = "black"; $baby->weight = 180; // a nice evening in the Bear family // daddy kills prey and brings it home $daddy->kill(); // mommy eats it $mommy->eat(); // and so does baby $baby->eat(); // mommy sleeps $mommy->sleep(); // and so does daddy $daddy->sleep(); // baby eats some more $baby->eat(); ?>

    It seems like it cuts off the script at the first use of "->" and prints the rest as regular text. This is what my source looks like.

    <html xmlns="http://www.w3.org/1999/xhtml"&gt;

    <head>
    <title>Untitled 1</title>
    </head>

    <body>
    <?php
    // PHP 5

    // class definition
    class Bear {
    // define properties
    public $name;
    public $weight;
    public $age;
    public $sex;
    public $colour;

    // define methods
    public function eat() {
    echo $this->name." is eating… ";
    }

    public function run() {
    echo $this->name." is running… ";
    }

    public function kill() {
    echo $this->name." is killing prey… ";
    }

    public function sleep() {
    echo $this->name." is sleeping… ";
    }
    }

    ?>

    <?php

    // my first bear
    $daddy = new Bear;
    // give him a name
    $daddy->name = "Daddy Bear";
    // how old is he
    $daddy->age = 8;
    // what sex is he
    $daddy->sex = "male";
    // what colour is his coat
    $daddy->colour = "black";
    // how much does he weigh
    $daddy->weight = 300;

    // give daddy a wife
    $mommy = new Bear;
    $mommy->name = "Mommy Bear";
    $mommy->age = 7;
    $mommy->sex = "female";
    $mommy->colour = "black";
    $mommy->weight = 310;

    // and a baby to complete the family
    $baby = new Bear;
    $baby->name = "Baby Bear";
    $baby->age = 1;
    $baby->sex = "male";
    $baby->colour = "black";
    $baby->weight = 180;

    // a nice evening in the Bear family
    // daddy kills prey and brings it home
    $daddy->kill();

    // mommy eats it
    $mommy->eat();
    // and so does baby
    $baby->eat();

    // mommy sleeps
    $mommy->sleep();
    // and so does daddy
    $daddy->sleep();

    // baby eats some more
    $baby->eat();

    ?> </body>

    </html>

    Sorry for the long post, but I have no idea what is going wrong. Thanks for the help!

  13. _____anonymous_____ Says:

    I don’t usually comment much but I have to say this is an excellent tutorial on classes, the php manual was far too advanced and I was left feeling dejected and thinking I would never learn classes. This tutorial has made me think again – thank you.

    Unlike some others on this thread I haven’t pasted any of the code anywhere, rather I am already thinking about how it relates to my current projects.

    Will be reading the next chapter tomorrow.

  14. _____anonymous_____ Says:

    The answer to your question regarding "->" is

    "Note the -> symbol used to connect objects to their properties or methods, and the fact that the $ symbol is omitted when accessing properties of a class instance."

    HTH

  15. madmhan84 Says:

    what’s the meaning of the symbol (->)

    Example: echo $this->name." is running… ";

    is this the same to:
    echo $this == name." is running…";

    I used obviously (==) and not (->)

    thanks…..

  16. phileasphogg Says:

    After monkeying around a bit, where I patched some unrelated bits together, I discovered that I could create functions and retrieve the values I had pre-assigned to them. If there are other noobs out there in need of a helping hand, I list the following code, which includes lots of made-up-by me functions, so remember that you too can give your fantasy free rein…

    Here is the whole script, below which I list the "output":

    <HTML>
    <HEAD>
    <BODY>
    <h3>Funny Functions <B>:</B>)</h3>

    <?php
    // PHP 5
    // class definition
    class Bear {
    // define properties
    public $name;
    public $weight;
    public $age;
    public $sex;
    public $colour;

    // define methods
    public function eat() {
    echo $this->name." is eating again! <br />";
    }
    public function eatlight() {
    echo $this->name." is eating with a spoon. <br />";
    }
    public function eatknife() {
    echo $this->name." is eating with a knife! <br />";
    }
    public function drool() {
    echo $this->name." is drooling over a dagger! <br />";
    }
    public function run() {
    echo $this->name." is running… <br />";
    }
    public function kill() {
    echo $this->name.", pray tell, is killing prey? <br />";
    }
    public function sleep() {
    echo $this->name." is sleeping on bottom. <br />";
    }
    public function sleepstop() {
    echo $this->name." is sleeping on top. <br />";
    }
    public function sleepsnot() {
    echo $this->name." stays awake all night! <br />";
    }
    }
    ?>

    <p>Is the code above this line being executed, the code below this line, or both (see the PHP code on the Notepad file)?</p>

    <p><I>Both form a whole!</I></p>

    <?php

    // my first bear
    $daddy = new Bear;
    // give him a name
    $daddy->name = "Daddy Bear";
    // how old is he
    $daddy->age = 8;
    // what sex is he
    $daddy->sex = "a female.";
    // what colour is his coat
    $daddy->colour = "black";
    // how much does he weigh
    $daddy->weight = 300;

    // give daddy a wife
    $mommy = new Bear;
    $mommy->name = "Mommy Bear";
    $mommy->age = 7;
    $mommy->sex = "a male.";
    $mommy->colour = "black";
    $mommy->weight = 310;

    // and a baby to complete the family
    $baby = new Bear;
    $baby->name = "Baby Bear";
    $baby->age = 1;
    $baby->sex = "a male.";
    $baby->colour = "black";
    $baby->weight = 180;

    // retrieve properties
    echo $baby->name." weighs ".$baby->weight." kilos. <br />";
    echo $baby->name." is ".$baby->sex. "<br />";
    echo $daddy->name." weighs ".$daddy->weight." kilos. <br />";
    echo $daddy->name." is ".$daddy->sex. "<br />";
    echo $mommy->name." weighs ".$mommy->weight." kilos. <br />";
    echo $mommy->name." is ".$mommy->sex. "<br />";

    // a nice evening in the Bear family
    // mommy kills prey and brings it home
    $mommy->kill();

    // daddy eats it
    $daddy->eat();
    // and so does baby
    $baby->eatlight();
    // see what mom is eating with!
    $mommy->eatknife();

    // mommy sleeps
    $mommy->sleepstop();
    // and so does daddy
    $daddy->sleep();
    // baby stays awake
    $baby->sleepsnot();

    // baby eats some more
    $baby->drool();

    ?>

    The "output" (and I only used about half of the available parameters):

    Baby Bear weighs 180 kilos.
    Baby Bear is a male.
    Daddy Bear weighs 300 kilos.
    Daddy Bear is a female.
    Mommy Bear weighs 310 kilos.
    Mommy Bear is a male.
    Mommy Bear, pray tell, is killing prey?
    Daddy Bear is eating again!
    Baby Bear is eating with a spoon.
    Mommy Bear is eating with a knife!
    Mommy Bear is sleeping on top.
    Daddy Bear is sleeping on bottom.
    Baby Bear stays awake all night!
    Baby Bear is drooling over a dagger!

    </BODY>
    </HTML>

  17. phileasphogg Says:

    Okay, I finally got something to run! I tried to make the following part run:

    <?php

    // create instance
    $baby = new Bear;
    $baby->name = "Baby Bear";
    $baby->weight = 1000;

    // now create another instance
    // this one has independent values for each property
    $brother = new Bear;
    $brother->name = "Brother Bear";
    $brother->weight = 1000;

    // retrieve properties
    echo $baby->name." weighs ".$baby->weight." units ";
    echo $brother->name." weighs ".$brother->weight." units ";

    // call eat()
    $baby->eat(100);
    $baby->eat(50);
    $brother->eat(11);

    // retrieve new values
    echo $baby->name." now weighs ".$baby->weight." units ";
    echo $brother->name." now weighs ".$brother->weight." units ";

    ?>

    But an error message said that the class "Bear" did not exist. So I figured that it needed the code immediately above this code in the tutorial (although oftentimes running two pieces of code in succession which refer to the same animal causes the process to conk out for some reason), which does introduce the class "Bear", so I copied that into the file just above the other code, and the result was that it worked just fine, although I needed to add some instances of this critter, <br /> in order to get the text to appear as desired (as it appears in the tutorial). The result is just as in the tutorial (which I will repeat here in case you are following this with bated breath…):

    Baby Bear weighs 1000 units
    Brother Bear weighs 1000 units
    Baby Bear is eating 100 units of food…
    Baby Bear is eating 50 units of food…
    Brother Bear is eating 11 units of food…
    Baby Bear now weighs 1150 units
    Brother Bear now weighs 1011 units

    I reckon I need to monkey around with this tutorial quite a lot to get things to run as I expect them to, but you are still welcome to point out anything obvious if you like.

    Thanks,
    phileasphogg

  18. phileasphogg Says:

    Hi,

    So far I have been able to execute all the stuff that Vikram has tossed at us, but this chapter has me stumped. I thought that maybe Vikram omitted some operations that he expected that we had absorbed by now, but if that is the case, then I am a poor student! I ran the very first hunk of code, as follows, precisely as listed here (inside the usual HTML tags, of course):

    <?php
    // PHP 5

    // class definition
    class Bear {
    // define properties
    public $name;
    public $weight;
    public $age;
    public $sex;
    public $colour;

    // define methods
    public function eat() {
    echo $this->name." is eating… ";
    }

    public function run() {
    echo $this->name." is running… ";
    }

    public function kill() {
    echo $this->name." is killing prey… ";
    }

    public function sleep() {
    echo $this->name." is sleeping… ";
    }
    }

    ?>

    And nothing, absolutely nothing, happened!

    I figured that what Vikram had maybe taken for granted I could pick up elsewhere, so I went searching and found the advice that it was necessary to define the items (constants) prior to invoking the class name, so I tried that (actually, Vikram begins this Part with precisely that type of code string, which apparently belongs to PHP 4 rather than to PHP 5), and it worked at least as far as naming the items to be named (I used a different setup than the above, but closer in fact to Vikram’s initial hunk of code above, although I ended up changing name values to numerical values, corresponding to the order of the arguments), but then the porcess "died" when it came to executing the part about functions ("name" is eating mulberries, running over hot coals, etc. :)). What gives?

    Here is the code that executed itself nicely up until the functions part:

    <?php
    // PHP 5

    define("name", "weight", "age", "sex", "color");
    // assigning value
    ${0} = "Daddy Bear <br />";
    // getting value back
    echo ${0};

    // assigning value
    ${1} = "100 lbs <br />";
    // getting value back
    echo ${1};

    // assigning value
    ${2} = "10 yrs <br />";
    // getting value back
    echo ${2};

    // assigning value
    ${3} = "Male <br />";
    // getting value back
    echo ${3};

    // assigning value
    ${4} = "Grizzly <br />";
    // getting value back
    echo ${4};

    // class definition
    class Bear {

    // define methods
    function eat() {
    echo $this->name." is eating… ";
    }

    function run() {
    echo $this->name." is running… ";
    }

    function kill() {
    echo $this->name." is killing prey… ";
    }

    function sleep() {
    echo $this->name." is sleeping… ";
    }
    }

    ?>

    Any suggestions???

    Thanks,

    phileasphogg

  19. _____anonymous_____ Says:

    Actually, PHP5 does have it, I used it and works.
    http://www.php.net/manual/en/language.oop5.visibility.php
    Perhaps you use an older version.

  20. _____anonymous_____ Says:

    If PHP has no public/private/protected attribute for CLASSES, why is it writen right below "Going Deeper":

    <?php
    // PHP 5

    // class definition
    public class Bear {

    Huh?

  21. _____anonymous_____ Says:

    jana2 said, "If you think about it, what would be the benefit of a non-public class? no one would be able to call it!"

    He/she was probably thinking of java: classes can be defined in other classes and only directly usable by them. For example, whatever Set implementations are given by Collections.singleton() or Collections.emptySet(), they’re not public classes.

  22. _____anonymous_____ Says:

    un-inheritable is a word but you do not need a hyphen. So yeah, neat tut on classes and PHP.

    *moves on to the next chapter*

  23. _____anonymous_____ Says:

    Thank you so much for an up to date, clearly written tutorial using simple examples that even us newbies can understand.

  24. jana2 Says:

    You don’t need to declare the class itself as public, the methods and properties within can be public or private and whatever but not the class itself. If you think about it, what would be the benefit of a non-public class? no one would be able to call it!

    Private methods and properties stop developers from calling or changing these from their code, changes and calls can only be made from within the class. This can’t work with the class itself because it needs to be called in order to be used!

  25. _____anonymous_____ Says:

    I ran this script, which I pick from this page;

    <?php
    // PHP 5

    // class definition
    public class Bear {

    // define public properties
    public $name;
    public $age;

    // more properties

    // define public methods
    public function eat() {
    echo $this->name." is eating…\n";
    // more code
    }

    // more methods
    }

    ?> <?php
    // PHP 5

    // class definition
    public class Bear {

    // define public properties
    public $name;
    public $age;

    // more properties

    // define public methods
    public function eat() {
    echo $this->name." is eating…\n";
    // more code
    }

    // more methods
    }

    but I got an error message as below;
    Parse error: syntax error, unexpected T_PUBLIC in c:\wamp\www\WebDev\test\PHP OOP\visibility_public_class.php on line 5

    this is a bit of my server description:
    Apache version : Apache/1.3.33 (Win32)
    PHP version : 5.0.4
    MySQL version : 4.1.10a-nt – extension : mysqli

    why is this happened? isn’t a class can be declared as public/private in PHP5?
    ?>