Classes and Objects


 [access_modifier] class {     [access_modifier] var variable;         .         .         .     [access_modifier] function function_name (arg1, arg2, ... argN)     {         statement     }         .         .         . } 

A class is a collection of variables and functionsin OOP terms, properties and methods. A class is a type, and you create objects of that type and store them in PHP variables.

Here's an example, the Person class, which stores the name of a person with the set_name method and returns that name with the get_name method.

 class Person {     var $name;     function set_name($text)     {         $this->name = $text;     }     function get_name()     {         return $this->name;     } } 

You can use the new statement to create an object of a class. You don't have to use the new statement when creating simple variables of the built-in types, such as integer or float, but you do when you want to create a variable that holds an object of a class.

For example, to create an object of the Person class in a variable named $doug, you'd do this:

 <?php     class Person     {         var $name;         function set_name($text)         {             $this->name = $text;         }         function get_name()         {             return $this->name;         }     }     $doug = new Person; ?> 

At this point, you've got an object of the Person class. You can access the methods of the Person class using the arrow operator, like this, where we're setting the name of the person to Douglas:

 $doug = new Person; $doug->set_name("Douglas");         .         .         . 

To access that name, you can use the get_name method:

 $doug = new Person; $doug->set_name("Douglas"); echo "The person's name is ", $doug->get_name(), "."; 

When you run this code, you get:

 The person's name is Douglas. 

By default, you can also access properties using the -> operator. Here's an example:

 <?php     class Person     {         var $name;         function set_name($text)         {             $this->name = $text;         }         function get_name()         {             return $this->name;         }     }     $doug = new Person;     echo "The person's name is ", $doug->name, "."; ?> 

You can access the methods and properties of an object by default, but sometimes that's not a good idea. You might want to have an accessor method that restricts access, like this example, which restricts the values you can assign to the name property:

 function set_name($text) {     if (strlen($text) <= 128){         $this->name = $text;     } } 

You can also make the properties and methods private to the Animal class with the PHP access modifiers:

  • public. Means "Accessible to all"

  • private. Means "Accessible in the same class"

  • protected. Means "Accessible in the same class and classes derived from that class"

By default, all methods and properties are public, so there's no restriction on accessing them from outside an object. If you declare the name property as private, that property can't be accessed from code outside the class itself. Here's how you use it:

 <?php     class Person     {         private $name;         function set_name($text)         {             $this->name = $text;         }         function get_name()         {             return $this->name;         }     }     $doug = new Person;     echo "The person's name is ", $doug->name, ".";    // Will not work. ?> 

Here's what you'd see:

[View full width]

The person's name is PHP Fatal error: Cannot access private property Person::$name in lion.php

You can also make methods private as well:

 class Person {     var $name;     private function set_name($text)     {         $this->name = $text;     }     function get_name()     {         return $this->name;     } } 

In this case, you can't access set_name outside an object of this class.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net