new self() vs new static() in PHP
What’s the difference between new self() and new static() in PHP? Or is it the same?
It is not the same. self refers the class in which the new keyword is actually written. static refers to the class in the hierarchy, where the method has been called. Look at the following example:
class Class_A {
public static function get_self() {
return new self();
}
public static function get_static() {
return new static();
}
}
class Class_B extends Class_A {}
echo get_class(Class_B::get_self()); // A
echo get_class(Class_B::get_static()); // B
echo get_class(Class_A::get_self()); // A
echo get_class(Class_A::get_static()); // A
Related Posts
- Install Sentry on Ubuntu 18.04 Using Nginx and Let's Encrypt Certificates
- Install Laravel in Cloud9 PHP Workspace
- Avoid WPForms Spam by Implementing a Custom Honeypot
- Redirect to First Child Page Using Genesis Framework
- Install Laravel 5.5 at Codeanywhere with PHP 7.1 and MySQL 5.7
- Tutorial: Ubuntu 18.04 LAMP Setup for WordPress