trait
is kind of solution to the famous multiple inheritance
problem in PHP.
Using traits we can access methods from different traits that we use in our classclass Mobile {
public function battery() {
echo 'Battery: MB_06, MF_02, MF_00' . PHP_EOL;
}
}
trait Laser {
public function power() {
echo 'Power: 10 mW' . PHP_EOL;
}
}
trait Projector {
public function range() {
echo 'Range: 2 M' . PHP_EOL;
}
}
class Galaxy extends Mobile {
use Laser;
use Projector;
}
$obj1 = new Galaxy;
$obj1->range();
$obj1->power();
$obj1->battery();
class A {
final public static function who() {
echo __CLASS__;
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::who();
Fatal error: Cannot override final method A::who()
parent
and grandparent
clasesclass A {
public static function who() {
echo __CLASS__;
}
}
class B extends A {
public static function who() {
parent::who();
}
}
One way to call grandparent's who
method is to use the grandparent class name A
class C extends B {
public static function who() {
A::who();
}
}
C::who();
Another way is to call who
method of parent class which also calls its parent class (which is grandparent for C
class)class C extends B {
public static function who() {
parent::who();
}
}
C::who();
Late Static Binding
gives us ability to call child class method from parent class.
In this example who()
method of child class will be called from parent class test()
methodclass A {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who(); // Here comes Late Static Bindings
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test();
Another example of late static binding: parent class uses child class property - tableName
class Model
{
protected static $tableName = 'Model';
public static function getTableName()
{
return static::$tableName;
}
}
class User extends Model
{
protected static $tableName = 'User';
}
echo User::getTableName(); // User
who()
method of the child class B
from parent class A
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
self::who();
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test();
The code will call who()
method of parent class and print its name A
If we need test()
to call who()
method of child class B
from parent class, then we can use late static binding
technique.
To achieve it, we use static
keyword:class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who(); // Here comes Late Static Bindings
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test();
function foo()
{
static $numOfCalls = 0;
$numOfCalls++;
echo "this function has been executed " . $numOfCalls . " times" . PHP_EOL;
}
foo();
foo();
foo();
After executing the ebove code, $numOfCalls
static variable gets incremented and the following text is printed:this function has been executed 1 times
this function has been executed 2 times
this function has been executed 3 times
constant can not be changed
static variable can not be accessed with the class object
dollar sign is needed to access static variable inside and outside of the class
constant name can not be a keyword, e.g. class (BUT: function, self, parent, static)
Similarities :
both of them are class members
both of them are accessible with "self" keyword inside the class
class Goodbye {
const LEAVING_MESSAGE = "Thank you for visiting US!\n";
public function byebye() {
echo self::LEAVING_MESSAGE;
}
}
$goodbye = new Goodbye();
// Access const variable with getter method
$goodbye->byebye();
// Access const variable with class name
echo Goodbye::LEAVING_MESSAGE;
// Access const variable directly with object
echo $goodbye::LEAVING_MESSAGE;
About class constants:
Constants cannot be changed once it is declared
Class constants are case-sensitive
A class constant is declared inside a class with the const keyword
It is recommended to name the constants in all uppercase letters
new
keyword followed by the class name(new Foo)->nonStaticMethod();
Another method is to use self
which comes after new
keyword(new self)->nonStaticMethod();
Complete example:class Foo {
public function nonStaticMethod()
{
return 'non-static';
}
public static function staticMethod()
{
// return (new Foo)->nonStaticMethod();
return (new self)->nonStaticMethod();
}
}
echo Foo::staticMethod();
The second method new self
is better, because if we want to rename the class later, we will not need to change the class name more than one placeclass Foo {
private $color;
public function bar() {
echo 'before';
$this->color = "blue";
echo 'after';
}
}
// Foo::bar();
// Deprecated
$obj = new Foo;
$obj::bar();
Deprecated: Non-static method Foo::bar() should not be called statically
php.net: In PHP 7, calling non-static methods statically is deprecated, and will generate an E_DEPRECATED warning. Support for calling non-static methods statically may be removed in the future