I was wondering if it's possible to dynamically access a static class from within a class property. I'm trying to access the properties in a nchild class, but I'm having to do something like this:
$error = $this->errorClass;
$error::myMethod ();
What I really want to do is this:
$this->errorClass::myMethod ();
I keep getting PHP Parse error: syntax error, unexpected '::' If this isn't possible, does anybody know of a somewhat similar way to do things that doesn't involve repeating myself for every class/method? Thanks for your time!
$this->errorClass::myMethod();
Please explain to the parser what you want? $temp = errorClass::myMethod(); $this->$temp; or $temp = $this->errorClass; $temp::myMethod();.
Because of such problems this is impossible.
The shortest you can use (1 single statement...) is:
${'_'.!$this->errorClass}::myMethod();
Related
I'm working on an existing code base and got back an object with an attribute that starts with a number, which I can see if I call print_r on the object.
Let's say it's $Beeblebrox->2ndhead. When I try to access it like that, I get an error:
Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$'
How can I get that attribute?
What about this :
$Beeblebrox->{'2ndhead'}
Actually, you can do this for pretty much any kind of variable -- even for ones that are not class properties.
For example, you could think about a variable's name that contains spaces ; the following syntax will work :
${"My test var"} = 10;
echo ${"My test var"};
Even if, obviously, you would not be able to do anything like this :
$My test var = 10;
echo $My test var;
No idea how it's working internally, though... And after a bit of searching, I cannot find anything about this in the PHP manual.
Only thing I can find about {} and variables is in here : Variable parsing -- but not quite related to the current subject...
But here's an article that shows a couple of other possiblities, and goes farther than the examples I posted here : PHP Variable Names: Curly Brace Madness
And here's another one that gives some additionnal informations about the way those are parsed : PHP grammar notes
I actually found out the answer from a coworker before I asked this, but couldn't find it on Google, so I wanted to post it here in case others have the same problem.
I can access that attribute like so:
$Beeblebrox->{'2ndhead'}
It's not really legal to have an attribute or variable that begins with a number, but somehow a dynamic reference like this makes it possible. Seems like a weird loophole in the language to me.
You can do something like this too:
$aux = '2ndhead';
$Beeblebrox->$aux;
class List {
public function hello()
{
return "hello";
}
}
$list = new List;
echo $list::hello();
Gives Error:
PHP Parse error: syntax error, unexpected 'List' (T_LIST), expecting identifier (T_STRING) in /home/WtGTRQ/prog.php on line 3
Changing "List" to "Lizt" fixes the issue.
I sadly understand that Php functions are case-insensitive, but I really don't want to make a List object a Lizt object... Is there some way to circumvent this without renaming my class?
List is a restricted PHP word.
You cannot use any of the following words as constants, class names, function or method names.
http://www.php.net/manual/en/reserved.keywords.php
--
To answer your question, you will have to change your list class name to something else. MyList, CarList, Listing, etc..
I am pulling my hair out on this one. Pretty new at PHP but this is so basic I just can't figure out where the problem is. Using the code snippet below as an example:
class LG_Activity_Processor {
// Activity Types
const STATUS_DRAFT = 'draft';
const STATUS_PUBLISH = 'publish';
...
private $STATUS_FUTURE = 'future';
define ("STATUS_PRIVATE" , 'private');
I had originally intended to just use the "const" construct as the variables are fully defined prior to run time and I just think the syntax is prettier than that ugly "define". The problem is while the definition of the const gives no errors, whenever I refer to the constant later in the class I get the following error message:
PHP Notice: Use of undefined constant STATUS_PUBLISH - assumed 'STATUS_PUBLISH'
Huh? Just to be clear, here's the syntax I used to reference the "const":
$core_fields ['post_status'] = STATUS_PUBLISH;
I even tried:
$core_fields ['post_status'] = $this->STATUS_PUBLISH;
No love. I then entered a state of despair and eventually tried "define". Same calling syntax but I changed the definition syntax to that as illustrated above for "STATUS_PRIVATE". This made things far worth as I now had a fatal error on the define line that looked like this:
PHP Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION
I gave up. I finally just defined the variable as a private variable (as in the example of STATUS_FUTURE) and then referred to it as:
$core_fields ['post_status'] = $this->STATUS_PUBLISH;
That works just like you'd expect it to but I can't help but feeling I've been cheated out of doing it the right way. Any ideas on how to make my code whole again?
You should do LG_Activity_Processor::STATUS_DRAFT when accessing it.
Take a look at the PHP manual, it gives you clear examples.
http://php.net/manual/en/language.oop5.constants.php
This is similar to Can PHP instantiate an object from the name of the class as a string?.
I'm using propel orm with PHP 5.2.17, and I want to store the name of a query class in the database, say "AuthorQuery", then use it to get a query object. There may be a different way to do this with propel, avoding the ::create() factory method. That solution would be welcome, but I'd rather to know if this is even possible with php (I won't be terribly surprised if the answer is "It's not").
Here's the problem. This will work:
$author_class = "Author";
$author = new $author_class();
Using the new keyword a string will get interpreted as the class name.
But I get syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM (that's referring to the ::) when I try to do it using a factory constructor instead:
$author_query_class = "AuthorQuery";
$author_query = $author_query_class::create(); // syntax error at the ::
Do I need an extra $ or something?
It turns out, this is not an issue for PHP 5.3+
Works exactly the way you describe (as far as I remember) with PHP5.3. However, if you still use 5.2, you can use reflection
$x = new ReflectionClass($author_query_class);
$author_query = $x->getMethod('create')->invoke(null);
Or just
$author_query = call_user_func(array($author_query_class, 'create'));
Worth to mention, that this is "much magic" and it will get hard to understand, what happens, when you have many of such constructions.
$author_query=call_user_func("$author_query_class::create");
This has to do with this line:
$author_query_class::create();
It is because (as quoted from the book PHP Master):
The double colon operator that we use for accessing static properties
or methods in PHP is technically called the scope resolution operator.
If there’s a problem with some code containing ::, you will often see
an error message containing T_PAAMAYIM_NEKUDOTAYIM. This simply refers
to the ::, although it looks quite alarming at first! “Paamayim
Nekudotayim” means “two dots, twice” in Hebrew.
You can do something like this in PHP 5.3 and more support is is on the table for PHP 5.4. Before PHP 5.2 you may have to use something like the other answers provided or , gulp, eval().
I'm working on an existing code base and got back an object with an attribute that starts with a number, which I can see if I call print_r on the object.
Let's say it's $Beeblebrox->2ndhead. When I try to access it like that, I get an error:
Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$'
How can I get that attribute?
What about this :
$Beeblebrox->{'2ndhead'}
Actually, you can do this for pretty much any kind of variable -- even for ones that are not class properties.
For example, you could think about a variable's name that contains spaces ; the following syntax will work :
${"My test var"} = 10;
echo ${"My test var"};
Even if, obviously, you would not be able to do anything like this :
$My test var = 10;
echo $My test var;
No idea how it's working internally, though... And after a bit of searching, I cannot find anything about this in the PHP manual.
Only thing I can find about {} and variables is in here : Variable parsing -- but not quite related to the current subject...
But here's an article that shows a couple of other possiblities, and goes farther than the examples I posted here : PHP Variable Names: Curly Brace Madness
And here's another one that gives some additionnal informations about the way those are parsed : PHP grammar notes
I actually found out the answer from a coworker before I asked this, but couldn't find it on Google, so I wanted to post it here in case others have the same problem.
I can access that attribute like so:
$Beeblebrox->{'2ndhead'}
It's not really legal to have an attribute or variable that begins with a number, but somehow a dynamic reference like this makes it possible. Seems like a weird loophole in the language to me.
You can do something like this too:
$aux = '2ndhead';
$Beeblebrox->$aux;