0

I'm getting error:

Fatal error: Method name must be a string in C:\xampp\htdocs\index.php on line 15"

LINE 15:

$obj = new $url[0]();

CODE:

<?php
error_reporting(E_ALL ^ E_NOTICE);

$url = $_GET['url'];
$url = explode('/', $url);

if (!file_exists('controllers/' . $url[0] . '.php')) {
    $url[0] = 'error';   // error kontroleris 
}    
require 'controllers/' . $url[0] . '.php';

$obj = new $url[0]();
$obj->$url[1]();

BTW.: Script is not finished yet.

1
  • 2
    What version of PHP is this? Commented Nov 8, 2012 at 17:56

2 Answers 2

2

Actually, this syntax:

$urls = array('DOMDocument');
$dom = new $urls[0]('');
var_dump( $dom );

... is valid even in PHP 5.2 (proof). But this line...

$obj->$url[1]();

... is pretty damn able (another proof) to throw exactly the same error as you've shown, as you don't check for url array length anywhere in your code.

Sign up to request clarification or add additional context in comments.

Comments

0
$_GET['url'] = 'foo/bar'; // temp set $_GET
$url = $_GET['url'];
$url = explode('/', $url);

if (file_exists('controllers/' . $url[0] . '.php')){
    require 'controllers/' . $url[0] . '.php';
}else{
    $url[0] = 'error'; 
}

$obj = new URL;
if(method_exists($obj,$url[0])){ // test that method exists
    echo $obj->$url[0](); // or whatever your handling may be
}

class URL{
    public function error(){
        $return = 'this is for the error handling';
        return $return;
    }
}

6 Comments

Now it says that there is an empty property and it cannot access it :/
are you ensuring that your using $obj->url and not $obj->$url
And then it says that function name must be a string :D
I've made some edits to the code, on the basis of what i think your trying to achieve
Now it just says "this is for the error handling" But it should display the code from two echo'ed controllers Maybe you have skype ? I would contact you
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.