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

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

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

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

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

?>


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.


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
 
Search...
 
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
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


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