-
-
Save the-ge/ce842e51b01a78dde69ad7f11a44083d to your computer and use it in GitHub Desktop.
A simple example of using a Lazy Loading Proxy object in PHP. It is useful when attempting to work with legacy code for logging and memory savings when working with global objects..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$Database = new LazyLoadingProxy('Database', '/home/simon/DatabaseClass.php'); | |
$Blog = new LazyLoadingProxy('Blog', '/var/www/classes/Blog.class.php5'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo $Database->getTable(); // echo the name of the table |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Simon Holywell <[email protected]> | |
*/ | |
class LazyLoadingProxy { | |
/** | |
* Where the instance of the actual class is stored. | |
* @var $instance object | |
*/ | |
private $instance = null; | |
/** | |
* The name of the class to load | |
* @var $class_name string | |
*/ | |
private $class_name = null; | |
/** | |
* The path to the class to load | |
* @var $class_path string | |
*/ | |
private $class_path = null; | |
/** | |
* Set the name of the class this LazyLoader should proxy | |
* at the time of instantiation | |
* @param $class_name string | |
*/ | |
public function __construct($class_name, $class_path = null) { | |
$this->setClassName($class_name); | |
$this->setClassPath($class_path); | |
} | |
public function setClassName($class_name) { | |
if(null !== $class_name) { | |
$this->class_name = $class_name; | |
} | |
} | |
public function getClassName() { | |
return $this->class_name; | |
} | |
public function setClassPath($class_path) { | |
if(null !== $class_path) { | |
$this->class_path = $class_path; | |
} | |
} | |
public function getClassPath() { | |
return $this->class_path; | |
} | |
/** | |
* Get the instance of the class this LazyLoader is proxying. | |
* If the instance does not already exist then it is initialised. | |
* @return object An instance of the class this LazyLoader is proxying | |
*/ | |
public function getInstance() { | |
if(null === $this->instance) { | |
$this->instance = $this->initInstance(); | |
} | |
return $this->instance; | |
} | |
/** | |
* Load an instance of the class that is being proxied. | |
* @return object An instance of the class this LazyLoader is proxying | |
*/ | |
private function initInstance() { | |
Logger::log('Loaded: ' . $class_name); | |
require_once($this->class_path); | |
$class_name = $this->class_name; | |
return new $class_name(); | |
} | |
/** | |
* Magic Method to call functions on the class that is being proxied. | |
* @return mixed Whatever the requested method would normally return | |
*/ | |
public function __call($name, $arguments) { | |
$instance = $this->getInstance(); | |
Logger::log('Called: ' . $this->class_name . '->' . $name . '(' . print_r($arguments, true) . ');'); | |
return call_user_func_array( | |
array($instance, $name), | |
$arguments | |
); | |
} | |
/** | |
* These are the standard PHP Magic Methods to access | |
* the class properties of the class that is being proxied. | |
*/ | |
public function __get($name) { | |
Logger::log('Getting property: ' . $this->class_name . '->' . $name); | |
return $this->getInstance()->$name; | |
} | |
public function __set($name, $value) { | |
Logger::log('Setting property: ' . $this->class_name . '->' . $name); | |
$this->getInstance()->$name = $value; | |
} | |
public function __isset($name) { | |
Logger::log('Checking isset for property: ' . $this->class_name . '->' . $name); | |
return isset($this->getInstance()->$name); | |
} | |
public function __unset($name) { | |
Logger::log('Unsetting property: ' . $this->class_name . '->' . $name); | |
unset($this->getInstance()->$name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment