Home Tutorial Php Phpoop PHP Access Specifiers

 
 

Share on Google+Share on Google+
PHP Access Specifiers
Posted on: February 20, 2010 at 12:00 AM
Advertisement
In the current tutorial we will get to know about different types of access specifiers or visibility in PHP. You will also come to know that what could be difference in public, private and protected access specifiers. What could be the difference if we use these keywords before a method and before variables. Examples will exemplify each of the above concepts

Abstract Class:

Abstract classes and methods are introduced in PHP 5. The concept behind the abstract class is that we need to extend this class by its descendant class(es). If a class contains abstract method then the class must be declared as abstract. Any method which is declared as abstract must not have the implementation part but the declaration part only.

The child classes which inherits the property of abstract base class, must define all the methods declared as abstract.

Example:

<?php

abstract class One{

public function disp(){

echo "Inside the parent class<br/>";

}

}

class Two extends One{

public function disp(){

echo "Inside the child class<br/>";

}

}

class Three extends One{

//no method is declared

}

$two=new Two();

echo "<b>Calling from the child class Two:</b><br/>";

$two->disp();

echo "<b>Calling from the child class Three:</b><br/>";

$three=new Three();

$three->disp(); 

?>

Output:

Calling from the child class Two:
Inside the child class
Calling from the child class Three:
Inside the parent class

 

Example:

<?php

abstract class One{

abstract function disp();

}

class Two extends One{

public function disp(){

echo "Inside the child class<br/>";

}

}

class Three extends One{

public function disp(){

echo "Inside the child class 2<br/>";}

}

$two=new Two();

echo "<b>Calling from the child class Two:</b><br/>";

$two->disp();

echo "<b>Calling from the child class Three:</b><br/>";

$three=new Three();

$three->disp();

?>

Output:

Calling from the child class Two:
Inside the child class
Calling from the child class Three:
Inside the child class 2

 

 

 

Advertisement

Related Tags for PHP Access Specifiers:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 20, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com