This question already has answers here:
PHP curly brace syntax for member variable
(5 answers)
Closed 9 years ago.
http://www.php.net/manual/en/functions.variable-functions.php#24931
That function does something like $this->{$this->varname}(). I tried it out and confirmed that that's valid syntax but it leaves me wondering... where does php.net discuss the use of curly brackets in variable names like that?
Variable variables:
Class properties may also be accessed using variable property names. ...
Curly braces may also be used, to clearly delimit the property name.
See examples on that page, too.
Why shouldn't it work?
These are variable variables/function names.
$f = "time";
$f(); // returns the actual time
It's now the same, only in object context (http://php.net/manual/en/functions.variable-functions.php):
$object->$f; // calls the method with the name $f in $object
Now, to say that it is the method with the name $this->varname, you need to write $this->{$this->varname} as $this->$this->varname will be interpreted as ($this->$this)->varname which results in $this->{$this->__toString()}->varname what you don't want.
Related
This question already has answers here:
php - Why can't you define a constant named EMPTY
(4 answers)
Closed 4 years ago.
Could you please tell me why this code throws a parse error if the name of the constant is EMPTY, but if I change it to EMPTY2 or SUBSTR it does work.
define('EMPTY', '');
if (empty(EMPTY)) {
echo 'hello world';
}
Because, as stated in this Quora answer, PHP function names are case-insensitive, so EMPTY collides with the built-in function empty().
PHP manual mentions this in a small note right after Example #3 in the subsection about User-defined functions:
Note: Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.
This question already has answers here:
How and why use curly braces: return $this->{$this->action}();
(2 answers)
Closed 6 years ago.
Feel free to re-title this Question because I do not know the proper name for doing this. More to the point, I have seen people using {'property'} when accessing a property inside an object so I set-up an example to try understand however, the property is accessible when I use it and when I don't?
class Example {
public $name;
}
$e = new Example();
$e->{'name'} = 'Kdot';
echo $e->name; // output: Kdot
I have tried changing the scopes and accessing it through a class method but it works both ways, again.
Can someone help me understand what the meaning of using the {} delimiters are? Because from my knowledge, if you stored the parameter inside another variable, this would also work:
$property = 'name';
echo $e->$property; // output: Kdot
They're mostly both just different ways to do the same thing, but with slightly different capabilities.
One difference is that if you want to concatenate inline and use that for a property name, you need the braces:
// example:
$property = 'foo';
echo $e->{$property . 'Suffix'}; // good, uses $e->fooSuffix
echo $e->$property . 'Suffix' // bad, uses $e->foo and adds "Suffix" literal
As well as that, directly access property names need to conform to certain rules. For example if you have a dash in your property name you need to use braces to access it.
This question already has answers here:
Should an array be declared before using it? [closed]
(7 answers)
Closed 7 years ago.
In most languages, I have to initialize an associative array before I can use it:
data = {}
data["foo"] = "bar"
But in PHP I can just do
data["foo"] = "bar"
Are there any repercussions to doing this? Is this "the right way" to write PHP?
Is the same, but is not a good idea, the next is a copy-paste from php documentation.
If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array. This practice is however discouraged because if $arr already contains some value (e.g. string from request variable) then this value will stay in the place and [] may actually stand for string access operator. It is always better to initialize variable by a direct assignment.
Basically it's the same, and no you won't find any problem or repercussion.
But if you like you can do this:
$a = array();
You can read more in the PHP page
This question already has answers here:
What is the difference between a language construct and a "built-in" function in PHP?
(4 answers)
Closed 8 years ago.
PHP has a large number of batteries-included functions, e.g. functions on arrays. Some of these, like each, are present in get_defined_functions()['internal']. Others, like reset and many others, are not present at all. However, they are treated as functions in every other way: they are not documented as "language constructs" or keywords; I can call them using the "variable function" feature; function_exists("reset") returns true; if I try to redefine them (e.g. function reset() { ... }), I get an error about redeclaration, rather than a syntax error; and so on.
Why are these functions not listed by get_defined_functions? Are they not actually functions? If not, what are they? If they are functions, then what actually is it that get_defined_functions is listing? In either case, how do I list the things that don't appear in get_defined_functions?
Quite a short answer: Reset is present in get_defined_functions()['internal'].
Look at [1532] in this fiddle: http://phpfiddle.org/main/code/h5n-ndx
This question already has answers here:
Curly braces in string in PHP
(4 answers)
Closed 1 year ago.
What is the difference between the following 2 queries?
mysql_query("UPDATE table SET name = '$name'");
mysql_query("UPDATE table SET name = '{$name}'");
ON the SQL side, there is absolutely no difference : the two queries are exactly the same.
(you can check that by echo-ing them)
{$variable} is a more complete syntax of $variable, that allows one to use :
"this is some {$variable}s"
"{$object->data}"
"{$array['data']}"
"{$array['data']->obj->plop['test']}"
For more informations, you should read the Variable parsing / Complex (curly) syntax section of the manual (quoting a few bits) :
This isn't called complex because the
syntax is complex, but because it
allows for the use of complex
expressions.
Any scalar variable, array element or
object property with a string
representation can be included via
this syntax. Simply write the
expression the same way as it would
appear outside the string, and then
wrap it in { and }.
The curly braces "escape" the PHP variable and are not passed to MySQL. With a simple variable like $name it doesn't make a difference but with something like $user['name'] it does. So there is nothing different between the two queries you have posted in your question.
This query can be used if you want to pass a single variable:
mysql_query("UPDATE table SET name = '$name'");
This can be used if you are passing a value from an array's particular index.
mysql_query("UPDATE table SET name = '{$1}'",$name);
By the way your both queries were also correct in their means.