Trying to access a required files array in a PHP Class constructor -
i making silly error have no idea why.
i include file right before class declaration :
require_once('assets.php') //php_include_path set correct folder , file loads class a{ function __construct(){ var_dump($assets); // dumps null } } in assets.php, have array this:
$assets['file'] = array('abc','qrd'); so why getting null here?
there 2 methods choose this, depending on on situation, can decide works best.
use assets argument class constructor. $assets available in constructor unless use class property below.
require_once('assets.php'); class a{ function __construct($assets){ var_dump($assets); } } $a = new a($assets); or
put require in constructor. example features class property can use $this->_assets in of class's methods.
class a{ protected $_assets; function __construct(){ require_once('assets.php'); $this->_assets = $assets; var_dump($this->_assets); } }
Comments
Post a Comment