4.2 Building Classes


Now that you have learned about the theoretical background of object-oriented software development, it is time to return to PHP-specific subject matter.

In this section you will learn to write your own PHP classes and you will see how inheritance and function overloading can be done in PHP.

4.2.1 A Simple Class

Writing classes in PHP is as simple as writing functions. However, some things have to be taken into consideration when working with classes and instances.

The following piece of code shows the class called human, which stores information all human beings have in common:

 <?php # Class human class human {         var $age;         var $gender;         # Constructor         function human()         {                 # an empty constructor         }         # Setting the age of a person         function setage($age)         {                 $this->age = $age;         }         function setgender($gender)         {                 $this->gender = $gender;         } } ?> 

The keyword class defines the name of the class, which can be found in curly brackets. The name of the class is defined as human. At the beginning of the class some values are defined using the keyword var. These values are the attributes of the class. After that you can see the implementation of a constructor. In this example the constructor has nothing to do.

Let's have a closer look at the script using the class:

 <?php         include("human.php");         # Generating two humans         $epi = new human();         $hans  = new human();         $epi->age = 25;         $hans->setage(23);         echo "Epi is $epi->age years old<br>\n";         echo "Hans is $hans->age years old<br>\n"; ?> 

After including the file containing the class, two instances of the class called human are created. In the next step the age of $epi is set to 25 by accessing an attribute of $epi. All attributes of the class have been defined in the code of the class using the keyword var. Another method of defining the age of a human in this example is to call the function called setage as was done for the instance $hans. setage modifies the value of the attribute to 23.

Finally, the values assigned to the instances are displayed on the screen:

 Epi is 25 years old Hans is 23 years old 

4.2.2 Objects and Scope

After you have seen how to implement a simple class, you'll have a closer look at what effect object orientation has on scope. As you have already seen, the idea behind object orientation is to combine functions related to a certain topic in an object. The same applies to the attributes related to an object. All attributes of an object can only be seen inside an object. If one of these variables has to be accessed from outside an object, it can only be done by accessing it via the instance of an object:

 <? # Class human class human {         var $age;         var $gender;         # Constructor         function human($age, $in_gender)         {                 $this->age = $age;                 $gender = $in_gender;         } } $hans = new human(23, 'm'); echo "age: $age<br>\n"; echo "age: $hans->age<br>\n"; echo "gender: $gender<br>\n"; echo "gender: $hans->gender<br>\n"; ?> 

At the beginning of the class, two attributes are defined. This time the constructor is designed to work with two parameters. The first parameter is assigned to $this->age. The second parameter is assigned to the local variable called $gender.

When creating the new instance, $hans, the constructor is called. Four variables are displayed on the screen. First $age is displayed. Then $hans->age is accessed. The same operations are performed for $gender as well.

When executing the script, you will see that only one variable is defined:

 age: age: 23 gender: gender: 

The first echo command displays an empty variable because $age has not been defined in the main program and $age in $hans cannot be accessed this way. The second echo command displays the desired result because $age is accessed via $hans. This way PHP knows which $age to access.

In the case of $gender, the echo command displays nothing for the same reasons we gave for the first echo command. The last echo command returns nothing as well because $in_gender has not been assigned to an attribute of $hans. $gender is treated as a local variable and is destroyed automatically at the end of the constructor. Many people forget to assign variables to the attributes of an instance; as you can see, this will lead to bugs and trouble.

4.2.3 Simulating Function Overloading

Function overloading is not supported by PHP explicitly. In general, a user can pass as many parameters to a constructor as he wants to. Therefore it is not useful to implement functions accepting a certain number of parameters.

Because PHP is a very flexible language, there is an easy way to get around function overloading by working with arrays:

 <? # Class human class human {         var $age;         var $gender;         var $income;         # Constructor         function human($data)         {                 $this->age = $data["age"];                 $this->gender = $data["gender"];                 $this->income = $data["income"];         } } $hans_data["age"] = 23; $hans_data["gender"] = 'm'; $hans_data["income"] = 25000; $hans = new human($hans_data); $epi_data["age"] = 25; $epi = new human($epi_data); echo "Hans - age: $hans->age<br>\n"; echo "Hans - gender: $hans->gender<br>\n"; echo "Hans - income: $hans->income<br><br>\n"; echo "Epi - age: $epi->age<br>\n"; echo "Epi - gender: $epi->gender<br>\n"; echo "Epi - income: $epi->income<br>\n"; ?> 

This time the constructor is designed to accept one parameter, which in this case is an array. All appropriate values in this array are assigned to the attributes of the class by the constructor. As you can see, $hans is generated by using an array containing all attributes supported by the class. In the case of $epi, this is different because you pass an array with just one value to the constructor.

When you execute the script using your favorite Web browser, you will see what comes out:

 Hans - age: 23 Hans - gender: m Hans - income: 25000 Epi - age: 25 Epi - gender: Epi - income: 

The result is no surprise. $epi has been created successfully, but not all of the attributes have been initialized with a useful value.

What happens when a constructor designed for one value is called with two values? Let's have a look at the next example:

 <? # Class human class human {         var $age;         var $gender;         var $income;         # Constructor         function human($age)         {                 $this->age = $age;         } } $hans = new human(23); $epi = new human(25, 'm'); echo "Hans: $hans->age<br>\n"; echo "Epi: $epi->age<br>\n"; ?> 

In the main program $hans is generated by calling the constructor with just one parameter. $epi, however, is generated by calling the constructor with two parameters. Let's see what comes out when the script is executed:

 Hans: 23 Epi: 25 

The second parameter is silently omitted by PHP and the program works as if you had called the constructor with just one parameter.

Let's modify the script and implement an additional constructor designed for accepting two parameters:

 <?php # Class human class human {         var $age;         var $gender;         # Constructor         function human($age, $gender)         {                 echo "constructor with two parameters<br>\n";                 $this->age = $age;                 $this->gender = $gender;         }         function human($age)         {                 echo "constructor with one parameter<br>\n";                 $this->age = $age;         } } $hans = new human(23); $epi = new human(25, 'm'); echo "Hans: $hans->age<br>\n"; echo "Epi: $epi->age<br>\n"; ?> 

As you can see, two constructors have been implemented. C++ programmers might think that PHP finds out which constructor has to be called depending on the number of parameters the constructor has to be called with, but this won't happen:

 constructor with one parameter constructor with one parameter Hans: 23 Epi: 25 

The second constructor is always called. In this example PHP won't display an error, but for people who are used to programming C++, the behavior of the interpreter seems strange.

4.2.4 Cloning Objects

After you have created an instance of an object, you might want to clone it. In many languages it is necessary to implement a copy-constructor. In the case of PHP, it is not necessary in most situations to implement an additional function because almost all cloning operations can be performed by using the = operator.

In this section you will have a closer look at how to treat more than just one instance of an object and how to clone objects.

Let's start with an example:

 <?php # Class human class creature {         var $name;         var $color;         function creature($name, $color)         {                 $this->name = $name;                 $this->color = $color;                 echo "creature called $name has been created<br>\n";         } } # creating two creatures $acreature = new creature('Kertal', 'white'); $ccreature = new creature('Broesl', 'yellow'); # bcreature references a creature $bcreature = &$acreature; # cloning acreature $ccreature = $acreature; # modifying acreature $acreature->color = 'black'; # displaying results echo '<br>'; echo "acreature: $acreature->name; $acreature->color<br>\n"; echo "bcreature: $bcreature->name; $bcreature->color<br>\n"; echo "ccreature: $ccreature->name; $ccreature->color<br>\n"; ?> 

First, a class called creature is implemented. It has two attributes and one constructor to create new instances of the object.

In the next step two instances of creature are created. The name of the first creature is $acreature and the name of the second instance is $bcreature. After creating the two instances of creature, $bcreature is assigned to a reference of $acreature. This means that $bcreature is not a separate object any more but is an alias for $acreature. Every modification of $acreature will affect $bcreature.

In the next step $ccreature is created. This time the object is not a reference of $acreature but an individual and independent object. If $acreature is modified, the changes won't affect $ccreature.

Finally, $acreature is modified and the attributes of the various objects are displayed on the screen:

 creature called Kertal has been created creature called Broesl has been created acreature: Kertal; black bcreature: Kertal; black ccreature: Kertal; white 

As you can see, $acreature and $bcreature contain the values while $ccreature is still white because it has been created as an independent object.



PHP and PostgreSQL. Advanced Web Programming2002
PHP and PostgreSQL. Advanced Web Programming2002
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 201

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