10

Here's what I want to do:

public function all($model) {
  $query = 'SELECT ' . implode(', ', $model::$fields) ....;
}

Called like this:

$thing->all(Account);

I get this error:

Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in /home/mark/public_html/*/account.php on line 15

When inspecting $model with var_dump it turns out its a string. In the the first example if I change $model to Account on the $query line it works fine.

How can a take a string and turn it back into a class?

Edit: Updated example and title to reflect the problem isn't with self.

Solution: Since I'm not using PHP5.3, I had to resort to using eval() to get what I wanted. Thanks everybody!

1
  • 1
    What version of php are you using? I think you are required to use php 5.3 to use a variable for the class name to access a static variable within that class. Commented Jan 4, 2010 at 17:55

6 Answers 6

28

Classes are not first-class citizens in PHP, as such they may not be stored in variables, passed as function arguments, or returned from functions.

However, PHP will let you simulate a first-class citizen by using a string containing the name of the class, in certain situations:

$class = "Account";

$instance = new $class(); // You can create instances

call_user_func(array($class, 'frobnicate')); // You can call static functions

That's about all in PHP < 5.3. However, with PHP 5.3, you can also:

$class::frobnicate(); // cleanly call static functions

$fields = $class::$fields; // access static variables
Sign up to request clarification or add additional context in comments.

5 Comments

"frobnicate" is my new favorite word.
Okay, I'm trying to access a static variable, so it looks like I'll need PHP 5.3. Thanks.
I shouldn't be giving this advice, since eval is evil... but you can with appropriate precautions use eval('return '.$class.'::$fields;') to get that static variable. Or use a static function instead. :)
That's exactly what I ended up doing. Agreed on the evilness, though ;)
Since PHP 5.5 you can also use the "class" static member to access the fully qualified name, for example Account::class. See: php.net/manual/en/…
1

I have also experienced receiving such Fatal Error: Class 'MyClass' not found when you're class has a specific namespace, then it's probably the namespacing. You need to also mention the namespace in your String variable.

$class = "App\MyClass"; // mention the namespace too
$instance = new $class();

Comments

0

See Wikipedia on the scope resolution operator. Especially see the section on PHP and Hebrew.

1 Comment

I get what T_PAAMAYIM_NEKUDOTAYIM. The wiki article doesn't explain why I can't do the above.
0

You cannot use self this way : it can only be used in a static context (i.e. inside a static method) to point to the class -- and not its name.

If you are working with non-static methods (seems you are), you should use $this, instead of self.


Actually, before PHP 5.3, you cannot use a static method/data with a "dynamic" (i.e contained in a variable) class name -- see the examples on the page Static Keyword : they only work with PHP 5.3, for that kind of manipulation.

Which means a portion of code like this one :

class ClassA {
    public static $data = 'glop';
}

$className = 'ClassA';
var_dump($className::$data);

Will not work with PHP < 5.3

2 Comments

I'm actually using inside of of a static class function, but it doesn't work if I pass the class name either.
@Mark : yes ; it's because you cannot do something like $className::$data with PHP < 5.3 ;; it's only possible with PHP >= 5.3
-1

I find this similar line works in my Laravel app:

$thing->all(new Account);

Comments

-2

Try '$this' instead of self.

Self does work this way in PHP. PHP thinks it encounters an unknown constant, which it can't find, and then it assumes it's a string containing 'self'.

Edit: Can you post the class and the code where you instantiate the object?

2 Comments

I don't think that's it - it doesn't work if I pass the class name either.
self does exist in php. self refers to the current objects class type for use in static context.

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.