Advertisement
Guest User

PHP Nastiness

a guest
Mar 5th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.17 KB | None | 0 0
  1. class BaseClass {
  2.     // This base class has a protected constructor, this means that you should be unable to instigate it (so, $hai =                new BaseClass();) unless you're inside the object itself.
  3.     // This is commonly used in the singleton pattern
  4.     protected function __construct(){
  5.         echo "Successfully loaded\n";
  6.     }
  7. }
  8.  
  9. class WorkingClass extends BaseClass{
  10.     private $baseClass;
  11.     // As this load is effectively "inside" the BaseClass, it can happily generate a copy of WorkingClass, bypassing the constructor (as you would expect)
  12.     static function load(){
  13.         return new self;
  14.     }
  15.  
  16.     // However, here, we're creating a BaseClass object. As we're creating a copy from what would be thought of as a public location, we would expect it to throw an error saying that we can't run the constructor as it's protected.
  17.     // In an evil twist of fate however, PHP decides that as we're calling it in something else that extends BaseClass, then we should have access to it's internal attributes (so, protected) and let's us do it.
  18.     // This. Is. Horrific.
  19.     public function ermWhat(){
  20.         $this->baseClass = new BaseClass();
  21.     }
  22. }
  23.  
  24. $workingClass = new WorkingClass();
  25. $workingClass->ermWhat();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement