Loading...

oops in php

Refer to the parent class in php

  1. Student :: $total_students
  2. self :: $total_students
  3. parents :: $total_students
  4. only works with class methods, not instances
  5. self:: and parent:: are replacements for ClassName
  6. not needed for static properties:already shared
  7. 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