lush_rainforest Posted September 18, 2015 Share Posted September 18, 2015 (edited) Why can't I use __construct while namespaces are present? I am trying to use $this, but it needs the constructor function to work. However I want to use namespaces, but when I use namespaces, the constructor function won't work at all. Here's my example. <?php namespace foo; use foo as foo; \foo\foo_bar::foo(); class foo_bar { function __construct() { $this->foobar(); } public static function foo() { print('This is foo'); } public function foobar() { print('<br />This is foobar'); } } Is the constructor being ignored while namespaces are present or something? I need help with this because I want to use both constructor and namespaces at the same time. Basically, I want to use the constructor to call the parent constructor from the main file first before I call the foo function. However, I guess I could just stuff everything in the foo function and call the foobar function within the foo function. I don't know. modified ---------------------- I guess it does matter. I am trying split the URL into 3 parts then assign the 3 parts with a variable using $this. Without that, I would have to assign a variable without $this. Edited September 18, 2015 by lush_rainforest Quote Link to comment https://forums.phpfreaks.com/topic/298204-constructor-wont-work-with-namespaces/ Share on other sites More sharing options...
hansford Posted September 18, 2015 Share Posted September 18, 2015 (edited) You are only calling a static method of the class. In order to call the constructor you need to create an object of the class. use foo as foo; //\foo\foo_bar::foo(); $foo = new \foo\foo_bar(); $foo::foo(); class foo_bar { function __construct() { $this->foobar(); } public static function foo() { print('This is foo'); } public function foobar() { print('<br />This is foobar'); } } Edited September 18, 2015 by hansford Quote Link to comment https://forums.phpfreaks.com/topic/298204-constructor-wont-work-with-namespaces/#findComment-1521064 Share on other sites More sharing options...
lush_rainforest Posted September 18, 2015 Author Share Posted September 18, 2015 You are only calling a static method of the class. In order to call the constructor you need to create an object of the class. use foo as foo; //\foo\foo_bar::foo(); $foo = new \foo\foo_bar(); $foo::foo(); class foo_bar { function __construct() { $this->foobar(); } public static function foo() { print('This is foo'); } public function foobar() { print('<br />This is foobar'); } } Thanks that helped me out. Is it possible to make \foo\foo_bar dynamic? I used to be able to do something like $dynamic = $_GET['url']; $foo = new $dynamic(); $foo->foo(); But it's a little different with OOP. Is there anything that is equivalent to that in OOP? Trying to call for different classes using the same file. Quote Link to comment https://forums.phpfreaks.com/topic/298204-constructor-wont-work-with-namespaces/#findComment-1521090 Share on other sites More sharing options...
maxxd Posted September 18, 2015 Share Posted September 18, 2015 What you've got should actually work just fine. You'll want to do some more safety checks on the intended class, but that's the general idea. Something along the lines of this, for instance: $class = isset($_GET['url']) ? $_GET['url'] : 'notAFile'; //check to make sure the value of $_GET['url'] isn't mainpulating the file path to allow the user // to hijack system files, etc... if(file_exists("{$this->_pathToIncludes}/{$class}.php")){ $this->_inst = new $class(); } $this->_inst->method(); Of course, this is assuming you're using an autoloader and the file containing the class is named the same as the class itself; otherwise you'll need to include the target file before instantiating the class. You can google autloader implementations that help with namespaces and the corresponding pathing structure - I don't have an example handy, but it's not too terribly tough. Quote Link to comment https://forums.phpfreaks.com/topic/298204-constructor-wont-work-with-namespaces/#findComment-1521103 Share on other sites More sharing options...
lush_rainforest Posted September 18, 2015 Author Share Posted September 18, 2015 What you've got should actually work just fine. You'll want to do some more safety checks on the intended class, but that's the general idea. Something along the lines of this, for instance: $class = isset($_GET['url']) ? $_GET['url'] : 'notAFile'; //check to make sure the value of $_GET['url'] isn't mainpulating the file path to allow the user // to hijack system files, etc... if(file_exists("{$this->_pathToIncludes}/{$class}.php")){ $this->_inst = new $class(); } $this->_inst->method(); Of course, this is assuming you're using an autoloader and the file containing the class is named the same as the class itself; otherwise you'll need to include the target file before instantiating the class. You can google autloader implementations that help with namespaces and the corresponding pathing structure - I don't have an example handy, but it's not too terribly tough. Yes, I know what I have is correct, but I need it in OOP style. If I try combining that with OOP, I get thrown Fatal error: Class 'test' not found in I need to escape the namespace before I can get into the class. If I remove the namespace, I get the correct results which doesn't throw me that fatal error. But if I have the namespace in there, I get the fatal error. This is what I have so far. test.php <?php namespace test; class test { function __construct() { print('This is a test construct'); } } index.php <?php $class = isset($_GET['url']) ? $_GET['url'] : 'notAFile'; //check to make sure the value of $_GET['url'] isn't mainpulating the file path to allow the user // to hijack system files, etc... if(file_exists("{$this->_pathToIncludes}/{$class}.php")) { require("{$this->_pathToIncludes}/{$class}.php"); $this->_inst = new $class(); } $this->_inst->method(); Quote Link to comment https://forums.phpfreaks.com/topic/298204-constructor-wont-work-with-namespaces/#findComment-1521145 Share on other sites More sharing options...
Solution scootstah Posted September 19, 2015 Solution Share Posted September 19, 2015 (edited) You need to include the namespace in the dynamic class name. if(file_exists("{$this->_pathToIncludes}/{$class}.php")){ $class = "\test\\" . $class; $this->_inst = new $class(); } Edited September 19, 2015 by scootstah Quote Link to comment https://forums.phpfreaks.com/topic/298204-constructor-wont-work-with-namespaces/#findComment-1521149 Share on other sites More sharing options...
lush_rainforest Posted September 19, 2015 Author Share Posted September 19, 2015 You need to include the namespace in the dynamic class name. if(file_exists("{$this->_pathToIncludes}/{$class}.php")){ $class = "\test\\" . $class; $this->_inst = new $class(); } Thank you so much. That worked. Quote Link to comment https://forums.phpfreaks.com/topic/298204-constructor-wont-work-with-namespaces/#findComment-1521154 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.