note 87945 deleted from language.oop5.autoload by crell
| From: | crell@php.net | Date: | Mon, 04 Apr 2022 16:10:40 +0000 |
| Subject: | note 87945 deleted from language.oop5.autoload by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218028@lists.php.net to get a copy of this message | ||
Note Submitter: zachera
----
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);
}
}
}
}
?>