Guest User

Untitled

a guest
Mar 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. <?php
  2. /**
  3. * What is the difference between new self and new static?
  4. * self refers to the same class in which the new keyword is actually written.
  5. *
  6. * static, in PHP 5.3's late static bindings,
  7. * refers to whatever class in the hierarchy you called the method on.
  8. *
  9. * In the following example, B inherits both methods from A.
  10. * The self invocation is bound to A because it's defined in A's implementation
  11. * of the first method, whereas static is bound to the called class (also see get_called_class()).
  12. */
  13.  
  14. class A {
  15. public static function get_self() {
  16. return new self();
  17. }
  18.  
  19. public static function get_static() {
  20. return new static();
  21. }
  22. }
  23.  
  24. class B extends A {}
  25.  
  26. echo get_class(B::get_self()); // A
  27. echo get_class(B::get_static()); // B
  28. echo get_class(A::get_self()); // A
  29. echo get_class(A::get_static()); // A
Add Comment
Please, Sign In to add comment