Saturday, March 28, 2009

Class inheritance example in Php

With a simple example you can understand the inheritance concept. Think about Human class. All human have mouth, can eat, have nose etc. If you say Man class, all Man subclass inherits the characters of Human parent class plus has his own characteristics. Similarly female subclass inherits Man parent class.

Inheritance indicates the relationship between classed. If we have a generic class and another class inherits the generic class then the derived class inherit the attributes and behaviors from their parent classes, and can introduce their own. The derived class is the more specialized version of the parent class.

With the extends keyword in declaration subclass is defined. Note that a subclass can only inherits one base class. If the parent class is not defined as final then inside the subclass inherited methods and attributes can be overridden. To access the parent class methods and attributes parent:: keyword can be used.

Below is the class inheritance example in php.

<?php
class BaseClass{
function display(){
echo "This is base class.";
echo "<br />";
}
}

class DerivedClass extends baseClass{
function display(){
echo "This is derived class.";
echo "<br />";
parent::display();
}
}
$derivedclass=new DerivedClass();
$derivedclass->display();
?>


And the output is,
This is derived class.
This is base class.

Related Documents

http://arjudba.blogspot.com/2009/03/class-concepts-and-usage-syntax-in-php.html

Class concepts and usage syntax in php

In php5 handling of objects is completely re-written and more features of object oriented concept is added. In this post I will write basic class definition and accessing variables, methods syntax.

Before proceed let's have an idea what is called class. A class is an abstract characteristics of a thing which includes thing's characteristics (properties) plus thing's behavior (operations).

In programming aspect a class contains a list of variables and functions. Variables represent attributes and functions represent what class objects can do.

Every class definition begins with the keyword class, followed by a class name, followed by a pair of curly braces. Within the braces class members and methods are defined. A pseudo-variable $this can be used within the class method which indicates the references to the calling object.

Note that $this variable is available if class method is called after creating a new instance. This can be also another object's instance. Without creating instance, if a method is called directly then $this variable will not be available.

Below is a simple example of usage class in php.

<html>
<body>
<?php
class MyClass1{
function display(){
if(isset ($this)){
echo '$this is defined = ';
echo get_class($this);
echo "<br />";
}else {
echo '$this is not defined.';
echo "<br />";
}

}
}

class MyClass2{
function display2(){
MyClass1::display();
}
}

$myclass1=new MyClass1();
$myclass1->display(); //$this will be available as function is called from an object context.
MyClass1::display(); //As we are not creating instance here so $this will not be available.

$myclass2=new MyClass2();
$myclass2->display2(); //$this will be available as function is called from an object context.
MyClass2::display2(); //As we are not creating instance here so $this will not be available.
?>
</body>
</html>


The output is,

$this is defined = MyClass1
$this is not defined.
$this is defined = MyClass2
$this is not defined.

Related Documents

Sunday, March 22, 2009

Control Structures in php

Php uses various control structures. The control structure used in php along with some examples are noted below.

1)if : If the expression specified within "if statement" evaluates to TRUE then PHP will execute statement, and if it evaluates to FALSE if block is ignored.
A simple example,

<?php
if ($a > $b)
echo "The value of a variable is bigger than the value of b variable";
?>

2)else : If the condition inside if statement is not met then else block is executed.

<?php
if ($a > $b) {
echo "A value is greater than B value.";
} else {
echo "A value is not greater than B value.";
}
?>

3)elseif/else if: "elseif" / "else if" extends the if statement to execute a different statement in case the original if expression evaluates to FALSE.
The following is an example:

<?php
if ($x > $y) {
echo "x is bigger than y";
} elseif ($x == $y) {
echo "x is equal to y";
} else {
echo "x is smaller than y";
}
?>

Note that elseif /else if is same if brace is used. The only difference is "else if" can't be used if colon is used. Below is an example that will show the difference between elseif and elseif.

<?php
//This one is incorrect
$x=3;
$y=2;
if($x>$y): echo "x is greater than y";
else if ($x==$y): echo "both are equal";
else: echo "y is greater than x";
endif;
?>

Parse error: parse error, expecting `':'' in C:\xampp\htdocs\Elseif.php on line 6

<?php
//This one is correct
$x=3;
$y=2;
if($x>$y): echo "x is greater than y";
elseif ($x==$y): echo "both are equal";
else: echo "y is greater than x";
endif;
?>

x is greater than y

4)while: As long as the while expression evaluates to true, the statement inside while statement executes. Below is an example.

<?php
$a=10;
while ($a<15){
echo $a;
$a++;
}
?>

This will print 1011121314 in the browser.

5)Do .. while: Almost same as while loop but it ensures that at least one time the statement will be executed as the checking is done at the end of the iteration.

Below is an example.

<?php
$a=10;
do{
echo $a;
}while ($a>14);
?>

It will print 10 in the browser.

6)for : The syntax of for loop is,
for (expr1; expr2; expr3)
statement

The first expression (expr1) is executed once at the beginning of the loop.

In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues. Loop terminate whenever this expression evaluates to FALSE. If multiple expression is given in expr2 checking is done by AND operation between them.

At the end of each iteration, expr3 is executed.

Below is an example of for loop which will print 20 in the browser.

<?php
for ($a=20,$b=5;$a>10, $b<6;$a++, $b++)
echo $a;
?<


7)foreach: foreach construct is specially made to handle arrays. For normal variables it can't be used. When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array.
The following example will produced output 1234.

<?php
$numarr=array(1,2,3,4);
foreach ($numarr as $value){
echo $value;
}
?>

8)break: break ends execution of the current for, foreach, while, do-while or switch structure. With break you can supply a numeric number which tells how many structure it will end. For example,
while(){
switch(){
case 1:
break 2;
case 2:
break 1;
}
}
In this case break 2 will exit both the switch and while.
break 1 will only exit switch.

9)continue: Continue is used within looping structures and do following.
-Skip the rest of the current loop iteration.
-Evaluates the condition.
-Increase the iteration number by 1.

As of switch statement, continue statement also take numeric number.

10)switch: The switch statement is similar to a series of IF statements but constructs is different.
Below is an example of switch .. case which will display a is 3 on the browser.

<?php
$a=3;
switch($a){
case 1:
echo "a is 1";
break;

case 2:
echo "a is 2";
break;

case 3:
echo "a is 3";
break;

default:
echo "a is neither 1 nor 2 nor 3";
}
?>

11)return: If return is used inside function, statement immediately ends execution of the current function, and returns its argument as the value of the function call. Note that return is not a function. If you use return($a) then value of variable a is returned.

12)goto: The goto operator can be used to jump to other instruction in the program. --The target place is specified by the label and a colon.
-goto is followed by that label.
Below is an example which will print "Goto statement test" in the browser.

<?php
goto l;
echo "This will not execute";

l:
echo "Goto statement test";
?>

Note that the goto operator is available since PHP 5.3.