Archive for the ‘OOP’ Category
How to access variables or methods of the parent class in php
use the double colon(::) and parent keyword to access the variables or methods of the parent class in php
-
class myparent {
-
function myfunction() {
-
echo "parent method";
-
}
-
}
-
-
class mychild extends myparent {
-
function myfunc()[
-
parent::myfunction();
-
}
-
}
How to access Static variables in php
To access Static variables;
-
class mystatic {
-
private static $staticvar;
-
-
public static function staticmethod{
-
return <strong>self::$staticvar</strong>;
-
}
-
}
Difference between $this vs self in class using php
$this - use this if you are accessing a variable or method without static keyword within a class
self - use this if you are accessing a static variable or constant within a class
Accessing Static Variable within Static method in a Class
Use self to access static variable or const within a class.
example:
-
class static_class {
-
-
private static static_variable = 'I am static ';
-
public static function getStaticVariable() {
-
self::$static_variable;
-
}
-
}