Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP - Making Class Object Global Throughout Entire Application?
    primarykey
    data
    text
    <p>Is there a simple way to make a class object global for an entire application in PHP? I need my class to only be able to be instantiated once throughout the entire application and have it working 100%.</p> <p>Thanks.</p> <p><strong>EDIT</strong></p> <p>Decided the Singleton pattern is the best idea for what I need, and was requested to add a bit of code so here it is:</p> <p>Using the class in my app:</p> <pre><code>function catp_check_request(){ $catp=catp::getInstance(); $dlinks=array(); $ddlinks=array(); foreach($catp-&gt;rawlinks-&gt;link as $rawlink){ $dellink="catp-del-{$rawlink-&gt;name}"; array_push($dlinks,$dellink); } // More stuff.. } </code></pre> <p>Declaring the class:</p> <pre><code>class catp { private static $instance; private $errors; private $status_messages; private $plugin_name; private $plugin_shortname; private $links; private $linksurl; private $rawlinks; private function __construct(){} public static function getInstance() { if (self::$instance == null) { self::$instance = new catp(); } return self::$instance; } public function catp(){ $this-&gt;errors=false; $this-&gt;plugin_name='CloakandTrack Plugin'; $this-&gt;plugin_shortname='CaTP'; $this-&gt;status_messages=array( 'updated' =&gt; 'You just updated', 'enabled' =&gt; 'You just enabled', 'disabled' =&gt; 'You just disabled', 'debug' =&gt; 'Plugin is in debug mode' ); $this-&gt;linksurl=dirname(__FILE__).'/links.xml'; $this-&gt;rawlinks=simplexml_load_file($this-&gt;linksurl); $this-&gt;links=$this-&gt;catp_parse_links($this-&gt;rawlinks); } // More functions.. } </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.
 

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