The PHP Online Conference 2021

Voting

Please answer this simple SPAM challenge: four minus one?
(Example: nine)

The Note You're Voting On

zachera
11 years ago
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);
            }
        }
    }
}
?>

<< Back to user notes page

To Top