Creating an Object


In the previous chunk, we created a class named Animal, but not much is going to happen with that class unless we create an object of that class. The simple variables we've seen in previous chapters can also have typesfor example, integer, float, and so on. A class is an object's type, but you don't usually work with types directlyyou create variables using them.

It's the same thing with classes and objects. You can use the new statement to create a variable, called an object, of a certain 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 Animal class in a variable named $lion, you'd do this:

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

Now you've got an object of the Animal class named $lion. You can access the methods of the Animal class using the arrow operator, like this, where we're setting the name of the Animal to Leo:

 $lion = new Animal; $lion->set_name("Leo");         .         .         . 

This stores "Leo" in the object's name property. To access that name from outside the object, you can use the get_name method, as we've already done:

 $lion = new Animal; $lion->set_name("Leo"); echo "The name of your new lion is ", $lion->get_name(), "."; 

When you run this code, you get:

 The name of your new lion is Leo. 

Interestingly, you can access properties from outside an object using the -> operator just as you can use it to access methods. For example, what if you wanted to read the $lion object's name property directly?

To read the name property directly from the $lion object without using the get_name method, you can simply use the expression $lion->name. That's all it takes; here's what it looks like in code:

 <?php     class Animal     {         var $name;         function set_name($text)         {             $this->name = $text;         }         function get_name()         {             return $this->name;         }     }     $lion = new Animal;     $lion->set_name("Leo");     echo "The name of your new lion is ", $lion->name, "."; ?> 

Just as before, when you run this code, you get:

 The name of your new lion is Leo. 

Using the -> operator, you can access both the methods of an object and its properties. Often it's a good idea to use methods, called accessor methods, to set property values, because you can restrict the type of data you want to store in a property, as here, where we're making sure that the name stored in the Animal class isn't too long:

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



    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