Loading...

oops in php

Inherited Static Behaviors in php

  1. staic properties and methods are inherited
  2. visibility modifiers function the same
  3. inherited static properties are shared variables
  4. changes to the parent value change subclass values
  5. changes to a subclass value change the parent value

Example:

<?php
class Student{
public static $grade=['Fresher','Junior','Senior'];
}
class PartTimeStudent extends Student{
}
echo PartTimeStudent ::$grades[0];//Fresher
PartTimeStudent ::$grade[]='Super Senior';
echo implode(',',Student::$grades);//Fresher,Junior,Senior,Super Senior

Example2:

<?php
class Student{
public static $total_students=0;
}
class PartTimeStudent extends Student{
}
Student::$total_students++;
Student::$total_students++;
Student::$total_students++;

PartTimeStudent :: $total_student++;
echo Student :: $total_students;//4
echo PartTimeStudent :: $total_students;//4