note 87945 modified in language.oop5.autoload by danbrown
| From: | danbrown@php.net | Date: | Thu, 01 Jan 2009 00:59:23 +0000 |
| Subject: | note 87945 modified in language.oop5.autoload by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-148737@lists.php.net to get a copy of this message | ||
I found out a neat way to centralize one single class which will give accessibility to other
classes. I also added a parameter to the __construct method which would be an array of classes you
want loaded. This isn't completely necessary, but it will stop "excessive memory" if
you're loading a bunch of unused classes.
<?php
class Bot {
private $classes = array (
'Socket' => "connection/class.Socket.php",
'Input' => "io/class.Input.php",
'Output' => "io/class.Output.php",
'Parse' => "io/parse/class.Parse.php"
);
public $Socket, $Input, $Output, $Parse; // Accessible by other classes
public function __construct($load=false){
if(is_array($load)){
foreach($load as $class){
if(isset($this->classes[$class])){
require($this->classes[$class]);
$this->$class = new $class($this);
}
}
} else {
foreach($this->classes as $class => $path){
require($path);
$this->$class = new $class($this);
}
}
}
}
?>
--was--
I was noted of excessive memory usage for my last note, so a good workaround would be to add a
parameter to the __construct method which would be an array of classes you want loaded. This
isn't completely necessary, but it will stop "excessive memory" if you're
loading a bunch of unused classes.
<?php
class Bot {
private $classes = array (
'Socket' => "connection/class.Socket.php",
'Input' => "io/class.Input.php",
'Output' => "io/class.Output.php",
'Parse' => "io/parse/class.Parse.php"
);
public $Socket, $Input, $Output, $Parse; // Accessible by other classes
public function __construct($load=false){
if(is_array($load)){
foreach($load as $class){
if(isset($this->classes[$class])){
require($this->classes[$class]);
$this->$class = new $class($this);
}
}
} else {
foreach($this->classes as $class => $path){
require($path);
$this->$class = new $class($this);
}
}
}
}
?>
http://php.net/manual/en/language.oop5.autoload.php