Logo         Community
The Company
Your Account
Contact Us
PHP 101 (part 7): The Bear Necessities
Dodge Bears. Learn OOP.

| Going Deeper |

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

// 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...\n";
        // 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.x 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.x object model does not include support for visibility, the class definition above would not work in PHP 4.x. Instead, you would need to use the following:

// 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...\n";

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

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

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


From the above, it should be clear that class properties and methods in PHP 4.x 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.


$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


$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




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.

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback