Jump to content

Constructor won't work with namespaces


Go to solution Solved by scootstah,

Recommended Posts

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 by lush_rainforest
Link to comment
https://forums.phpfreaks.com/topic/298204-constructor-wont-work-with-namespaces/
Share on other sites

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 by hansford

 

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.

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.

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();
  • Solution

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 by scootstah
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.