tags:

views:

342

answers:

4

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!

A: 

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?

Jimmy Shelter
I don't think that's it - it doesn't work if I pass the class name either.
Mark A. Nicolosi
self does exist in php. self refers to the current objects class type for use in static context.
simplemotives
Yup, I realized my mistake and edited my answer. Thanks.
Jimmy Shelter
+1  A: 

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

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

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
Victor Nicollet
"frobnicate" is my new favorite word.
Peter Bailey
Okay, I'm trying to access a static variable, so it looks like I'll need PHP 5.3. Thanks.
Mark A. Nicolosi
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. :)
Victor Nicollet
That's exactly what I ended up doing. Agreed on the evilness, though ;)
Mark A. Nicolosi
+1  A: 

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

Justice
I get what T_PAAMAYIM_NEKUDOTAYIM. The wiki article doesn't explain why I can't do the above.
Mark A. Nicolosi