minik Posted March 13, 2011 Share Posted March 13, 2011 I'm currently learning to work with PHP classes for the first time properly and to do this, I'm developing a lightweight framework but I'm having trouble calling certain other classes within a class. I've got a "core" class which is the main one and is run when the index file is loaded. Inside this class' constructor, I have$this->load = new Load;The controller file is then included and the required function is run. However, when I try and call $this->load->library() from inside the controller class, I get an "undefined property" error. I know this is probably because they're separate classes but is there a way to run a function from the core class?I hope I've explained this properly so thanks for any help. Link to comment Share on other sites More sharing options...
Ingolme Posted March 13, 2011 Share Posted March 13, 2011 Did you declare the "load" property as public outside the constructor? Link to comment Share on other sites More sharing options...
minik Posted March 13, 2011 Author Share Posted March 13, 2011 I tried but it's still not working. Here's what I have so far (I've taken unnecessary bits out):index.php <?php// Load the core class.require 'core/core.php';$core = new Core;$core->run(); core/core.php <?phprequire 'core/load.php';class Core { function __construct() { $this->load = new Load; } function run() { $controller_name = 'hello_world'; $function = 'hello_universe' $controller_file = 'app/controllers/'.$controller_name.'.php'; $this->controller = new $controller_name_uc; call_user_func(array($this->controller, $function)); }} core/load.php <?phpclass Load { public function library($library) { if(is_array($library)) { foreach($library as $lib) { $this->library($lib); } } echo $library; }} app/controllers/hello_world.php <?phpclass Hello_world { function __construct() { echo $this->load->library('demo'); } function index() { echo 'Hello, world!'; }} Link to comment Share on other sites More sharing options...
birbal Posted March 13, 2011 Share Posted March 13, 2011 $this points to instance of current class. echo $this->load->library('demo');instance of Hello_world object calling load->libary() which is not defined in Hello_world class. Link to comment Share on other sites More sharing options...
minik Posted March 13, 2011 Author Share Posted March 13, 2011 Is there a way to get it to inherit the parent class? Like how MVC frameworks do? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 13, 2011 Share Posted March 13, 2011 http://www.php.net/manual/en/language.oop5.inheritance.php Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.