Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>1) use __autoload or <a href="http://php.net/manual/en/function.spl-autoload-register.php" rel="nofollow noreferrer">spl_autoload_register</a> to load classes</p> <p>2) use magic methods, to call function when getting unknown property. Following examples demonstrates how to use __get and dynamicaly initialize object only when you use them.</p> <pre><code>//use __autoload to load db and config class when they are called. class db{ function lol(){ echo 'Hello from db-&gt;lol() &lt;br /&gt;'; } } class config{ function lol(){ echo 'Hello from config-&gt;lol() &lt;br /&gt;'; } } //Manager class to use with classes where you want to access other object trough $this class Manager{ private $_instances=array(); function __get($name){ //if instance does not exists, create one if (!isset($this-&gt;_instances[$name])){ $this-&gt;_instances[$name]=new $name(); } //return instance return $this-&gt;_instances[$name]; } } class Some extends Manager{ function f1(){ $this-&gt;db-&gt;lol(); $this-&gt;config-&gt;lol(); } } $some=new Some(); $some-&gt;f1(); //echoes 'Hello from db-&gt;lol()' and 'Hello from config-&gt;lol()' </code></pre> <p>But for accessing global class instances I prefer using following method: Use singleton pattern to access global class trough GloballClass::i() and if global class is not defined use autoload to load that class.</p> <pre><code>class db extends mysqli{ private static $_i; //Access to singleton instance public static function i() { return (self::$_i instanceof self)?self::$_i:self::$_i = new self(); } //class functions function q($q){ echo 'Hello from db-&gt;q()'; } } class config{ private static $_i; //Access to singleton instance public static function i() { return (self::$_i instanceof self)?self::$_i:self::$_i = new self(); } //class functions function somefunction(){ echo 'Hello from config-&gt;somefunction()'; } } db::i()-&gt;q('SELECT * FROM users'); config::i()-&gt;somefunction(); </code></pre> <p>Following is solution inspired by Gordons comment: It uses GlobalClassFactory class to define only one instance of global classes.</p> <pre><code>class db{ function lol(){ echo 'Hello from db-&gt;lol() &lt;br /&gt;'; } } class config{ function lol(){ echo 'Hello from config-&gt;lol() &lt;br /&gt;'; } } class GlobalClassFactory{ private static $_classes=array(); public static function getInstance($name){ if (!isset(self::$_classes[$name])){ self::$_classes[$name]=new $name(); } return self::$_classes[$name]; } } class Base{ function __get($name){ return GlobalClassFactory::getInstance($name); } } class Some extends Base{ function f1(){ $this-&gt;db-&gt;lol(); $this-&gt;config-&gt;lol(); } } $some=new Some(); $some-&gt;f1(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload