1. Access specifier
PHP supports one error control operator: the at sign (@).When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.
3. Keyword static
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class.A property declared as static cannot be accessed with an instantiated class object (though a static method can).
class Foo {
public static function aStaticMethod() {
// ...
}
}
Foo::aStaticMethod();
$classname = 'Foo';
$classname::aStaticMethod(); // As of PHP 5.3.0
4. Constructor
PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
Destructor
PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.
class MyDestructableClass {
function __construct() {
print "In constructor\n";
$this->name = "MyDestructableClass";
}
function __destruct() {
print "Destroying " . $this->name . "\n";
}
}
$obj = new MyDestructableClass();
5. Overloading
Overloading in PHP provides means to dynamically "create" properties and methods.These dynamic entities are processed via magic methods one can establish in a class for various action types.
The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope.The rest of this section will use the terms "inaccessible properties" and "inaccessible methods" to refer to this combination of declaration and visibility.
All overloading methods must be defined as public.
class parentClass {
public function sum($a,$b){
echo $a+$b;
}
}
class childClass extends parentClass{
function sum($a,$b,$c) //Overloading function sum
{
$prod=$a+$b+$c;
echo $prod;
}
}
$a = new childClass();
$a->sum(2,3,4);//it will print 9
6. Overriding
Method overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class.
The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed.
class parentClass {
public function calculate($a,$b){
echo $a+$b;
}
}
class childClass extends parentClass{
function calculate($a,$b) //overriding function calculate
{
$prod=$a*$b;
echo $prod;
}
}
$a = new parentClass();
$a->calculate(2,3);//it will print 5
$a = new childClass();
$a->calculate(2,3);//it will print 6
7. Final Keyword
Final keyword, prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
class BaseClass {
public function test() {
echo "BaseClass::test() called\n";
}
final public function moreTesting() {
echo "BaseClass::moreTesting() called\n";
}
}
class ChildClass extends BaseClass {
public function moreTesting() {
echo "ChildClass::moreTesting() called\n";
}
}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()
Note: Properties cannot be declared final, only classes and methods may be declared as final.
8. Magic Methods
The function names __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() and __debugInfo() are magical in PHP classes.
We cannot have functions with these names in any of our classes unless we want the magic functionality associated with them.
Magic Method Description
__construct : This magic methods is called when someone create object of your class. Usually this is used for creating constructor in php5.
__destruct : This magic method is called when object of your class is unset. This is just opposite of __construct
__get : This method called when your object attempt to read property or variable of the class which is inaccessible or unavailable.
__set : This method called when object of your class attempts to set value of the property which is really inaccessible or unavailable in your class.
__isset : This magic methods trigger when isset() function is applied on any property of the class which isinaccessible or unavailable.
__unset : __unset is something opposite of isset method. This method triggers when unset() function called on inaccessible or unavailable property of the class.
__call : __call magic method trigger when you are attempting to call method or function of the class which is either inaccessible or unavailable.
__callstatic : __callstatic execture when inaccessible or unavailable method is in static context.
__sleep : __sleep methods trigger when you are going to serialize your class object.
__wakeup : __wakeup executes when you are un serializing any class object.
__toString : __toString executes when you are using echo on your object.
__invoke : __invoke called when you are using object of your class as function
9. goto
The goto operator can be used to jump to another section in the program. The target point is specified by a label followed by a colon, and the instruction is given as goto followed by the desired target label. This is not a full unrestricted goto. The target label must be within the same file and context, meaning that you cannot jump out of a function or method, nor can you jump into one. You also cannot jump into any sort of loop or switch structure.
You may jump out of these, and a common use is to use a goto in place of a multi-level break.
for($i=0,$j=50; $i<100; $i++) {
while($j--) {
if($j==17) goto a;
}
}
echo "i = $i";
a:
echo 'j hit 17';
10. Polymorphism
According to the Polymorphism principle, methods in different classes that do similar things should have the same name.
interface Shape {
public function calcArea();
}
class Circle implements Shape {
private $radius;
public function __construct($radius){
$this -> radius = $radius;
}
// calcArea calculates the area of circles
public function calcArea(){
return $this -> radius * $this -> radius * pi();
}
}
class Rectangle implements Shape {
private $width;
private $height;
public function __construct($width, $height)
{
$this -> width = $width;
$this -> height = $height;
}
// calcArea calculates the area of rectangles
public function calcArea()
{
return $this -> width * $this -> height;
}
}
$circ = new Circle(3);
$rect = new Rectangle(3,4);
echo $circ -> calcArea();
echo $rect -> calcArea();
- Class Members declared public can be accessed everywhere.
- Class Members declared protected can be accessed only within the class itself and by inherited classes.
- Class Members declared as private may only be accessed by the class that defines the member.
PHP supports one error control operator: the at sign (@).When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.
3. Keyword static
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class.A property declared as static cannot be accessed with an instantiated class object (though a static method can).
class Foo {
public static function aStaticMethod() {
// ...
}
}
Foo::aStaticMethod();
$classname = 'Foo';
$classname::aStaticMethod(); // As of PHP 5.3.0
4. Constructor
PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
Destructor
PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.
class MyDestructableClass {
function __construct() {
print "In constructor\n";
$this->name = "MyDestructableClass";
}
function __destruct() {
print "Destroying " . $this->name . "\n";
}
}
$obj = new MyDestructableClass();
5. Overloading
Overloading in PHP provides means to dynamically "create" properties and methods.These dynamic entities are processed via magic methods one can establish in a class for various action types.
The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope.The rest of this section will use the terms "inaccessible properties" and "inaccessible methods" to refer to this combination of declaration and visibility.
All overloading methods must be defined as public.
class parentClass {
public function sum($a,$b){
echo $a+$b;
}
}
class childClass extends parentClass{
function sum($a,$b,$c) //Overloading function sum
{
$prod=$a+$b+$c;
echo $prod;
}
}
$a = new childClass();
$a->sum(2,3,4);//it will print 9
6. Overriding
Method overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class.
The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed.
class parentClass {
public function calculate($a,$b){
echo $a+$b;
}
}
class childClass extends parentClass{
function calculate($a,$b) //overriding function calculate
{
$prod=$a*$b;
echo $prod;
}
}
$a = new parentClass();
$a->calculate(2,3);//it will print 5
$a = new childClass();
$a->calculate(2,3);//it will print 6
7. Final Keyword
Final keyword, prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
class BaseClass {
public function test() {
echo "BaseClass::test() called\n";
}
final public function moreTesting() {
echo "BaseClass::moreTesting() called\n";
}
}
class ChildClass extends BaseClass {
public function moreTesting() {
echo "ChildClass::moreTesting() called\n";
}
}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()
Note: Properties cannot be declared final, only classes and methods may be declared as final.
8. Magic Methods
The function names __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() and __debugInfo() are magical in PHP classes.
We cannot have functions with these names in any of our classes unless we want the magic functionality associated with them.
Magic Method Description
__construct : This magic methods is called when someone create object of your class. Usually this is used for creating constructor in php5.
__destruct : This magic method is called when object of your class is unset. This is just opposite of __construct
__get : This method called when your object attempt to read property or variable of the class which is inaccessible or unavailable.
__set : This method called when object of your class attempts to set value of the property which is really inaccessible or unavailable in your class.
__isset : This magic methods trigger when isset() function is applied on any property of the class which isinaccessible or unavailable.
__unset : __unset is something opposite of isset method. This method triggers when unset() function called on inaccessible or unavailable property of the class.
__call : __call magic method trigger when you are attempting to call method or function of the class which is either inaccessible or unavailable.
__callstatic : __callstatic execture when inaccessible or unavailable method is in static context.
__sleep : __sleep methods trigger when you are going to serialize your class object.
__wakeup : __wakeup executes when you are un serializing any class object.
__toString : __toString executes when you are using echo on your object.
__invoke : __invoke called when you are using object of your class as function
9. goto
The goto operator can be used to jump to another section in the program. The target point is specified by a label followed by a colon, and the instruction is given as goto followed by the desired target label. This is not a full unrestricted goto. The target label must be within the same file and context, meaning that you cannot jump out of a function or method, nor can you jump into one. You also cannot jump into any sort of loop or switch structure.
You may jump out of these, and a common use is to use a goto in place of a multi-level break.
for($i=0,$j=50; $i<100; $i++) {
while($j--) {
if($j==17) goto a;
}
}
echo "i = $i";
a:
echo 'j hit 17';
10. Polymorphism
According to the Polymorphism principle, methods in different classes that do similar things should have the same name.
interface Shape {
public function calcArea();
}
class Circle implements Shape {
private $radius;
public function __construct($radius){
$this -> radius = $radius;
}
// calcArea calculates the area of circles
public function calcArea(){
return $this -> radius * $this -> radius * pi();
}
}
class Rectangle implements Shape {
private $width;
private $height;
public function __construct($width, $height)
{
$this -> width = $width;
$this -> height = $height;
}
// calcArea calculates the area of rectangles
public function calcArea()
{
return $this -> width * $this -> height;
}
}
$circ = new Circle(3);
$rect = new Rectangle(3,4);
echo $circ -> calcArea();
echo $rect -> calcArea();