oops in php
Refer to the parent class in php
- Student :: $total_students
- self :: $total_students
- parents :: $total_students
- only works with class methods, not instances
- self:: and parent:: are replacements for ClassName
- not needed for static properties:already shared
- useful for calling static methods after overriding them
Example
<?php
class Chef{
public static function make_dinner(){
echo 'cook food';
}
}
class AmateurChef extends Chef{
echo 'Read recipe';
parent::make_dinner(){
echo 'clean up mess';
}
}
Chef:: make_dinner()
AmateurChef ::make_dinner();
Output
cook food
Read recipe
cook food
clean up mess
Example2:
<?php
class Image{
public static $resizing_enabled=true;
public static function geometry()
{
echo '800x600';
}
}
class ProfileImage extends Image{
public static function geometry()
{
if(self::resizing_enabled)
{
echo '100x100';
}
else
{
parent::geometry();
}
}
}
Image:: geometry();//800x800
ProfileImage::geometry();//100x100
Image::$resizing_enabled=false;
ProfileImage::geometry();//800x600