Extending classes
It’s also possible to have a second class which extends the main class. This make the class able to use the (public) functions (methods) & properties of the main class inside the other class. To extend a class we use extends.
Example:
class BasicClass {
}
class Class2 extends BasicClass {
}
Here Class2 extends the class ‘BasicClass’ which means it will be able to use all the functions and variables/constabnts of this class. However in this example those are none.
Using parent functions inside extending class
[b]Note:[/b] The examples in this tutorial are just to show how extending classes works and what possiblities it has, what it’s cappable of.
As mentioned above it’s possible to use functions of the basic class ( parent class ) inside the extending class ( which extends the basic class ). Here we’ll be doing this. Let’s say we’ve got a basic class which has functions to set the name, age and gender of a human (/colleagues for example). Now we could have multiple other classes which make use of these functions ( extend the basic class ), each representing ne colleague/human.
Note: this is just an example to show how it works and how extending classes can make use of functions set in a basic class.
Example:
class Colleague {
protected function setName($name) {
$this->name = $name;
}
protected function setGender($gender) {
$this->gender= $gender;
}
protected function setAge($age) {
$this->age= $age;
}
}
class Colleague1 extends Colleague{
parent::setName("Peter");
parent::setGender("Male");
parent::setAge("22");
}
class Colleague2 extends Colleague{
parent::setName("Kim");
parent::setGender("Female");
parent::setAge("20");
}
However this might not be such practical to do, a more practical use ( example ) is shown below. But this example is though a good example to easily understand what it’s cappable of ( extending classes ) theoretical and how it can be used.
Ok, so here we created one basic class which indicate the basic functions and properties for a colleage. Each colleague was made a class for ( 2 colleagues in this example ) using these functions to set the basic properties for each colleague. Let’s first have a look at the ’setName’ function of the basic ( parent ) class ‘Colleague’:
protected function setName($name) {
$this->name = $name;
}
It’s a protected function so only the extending class can use it. It sets the variable ‘name’ which will also be able to be used in the extending class ( as that one makes use of all functions AND properties/variables of the parent/basic class ). You could as well make it a public function so that the user can use it outside the class to set the name of the colleague manually ( as done in the 2nd example/part of this tutorial ). Here though we made it a protected function so we’ll use it inside the extending class which brings us to the extending class ‘Colleague1′ which sets the name with the following code:
parent::setName("Peter");
Or mainly:
parent::setName("name");
This uses the parent and :: symbols to access the parent class ( the class that it extends: ‘Colleague’ ). This method works like this:
parent::functionName();
To access a function of the parent class and:
parent::$variablename;
to access a variable of the parent class.
The parent class just uses $this->variableName to set the variables as they’re “global” – able to being used in any other class that extends this class.
Example of output from object:
<?php
class Colleague {
protected function setName($name) {
$this->name = $name;
}
protected function setGender($gender) {
$this->gender= $gender;
}
protected function setAge($age) {
$this->age= $age;
}
}
class Colleague1 extends Colleague{
parent::setName("Peter");
parent::setGender("Male");
parent::setAge("22");
}
class Colleague2 extends Colleague{
parent::setName("Kim");
parent::setGender("Female");
parent::setAge("20");
}
########CREATE OBJECT (OUTPUT EXAMPLE)########
$coll1 = new colleague1;
$coll1->name; //outputs the name which is set using the parent function setName and is set to 'Peter' in this class
$coll2 = new colleague2;
$coll2->name; //outputs the name which is set using the parent function setName and is set to 'Kim' in this class
?>
Using parent functions outside the extending class ( as object )
It’s also possible to have a basic class set the functions that the extending class needs to be cappable of. For example: SHOWING the name, instead of SETTING the name. Then we could have the class that extends it, create a function to set the name. So when an object of the class is created, we can set the name and use the function of the parent class as well to show the name. However we do need to make the functions of the basic class ( parent ) public as we want to use it outside the class ( wih the object, to show the name ).
class Colleague{
public function showName() {
return $this->name;
}
}
class Colleague1 extends Colleague{
public function setName($name) {
$this->name = $name;
}
}
So the extending class has his function to set the name, and uses the function of the basic class to be cappable of showing the name. We could create an object that shows us that it’s cappable of this:
$coll = new colleague1;
$coll->setName("Peter"); //set name, uses the function set in this extending class
echo $coll->showName(); //show name, uses the function set in the parent class (basic class: Colleague)


Mon, Feb 22, 2010
PHP & MySql