I'd like to have a reference stating clearly where PHP's arrow / method call operator (->) falls as regards the order of operator binding.
Unfortunately, the authoritative PHP manual page on operator precedence doesn't list it.
Example where someone could have doubts whether this could throw an exception because $price was first cast to string and only then the method call ->times() attempted:
return (string) $price->times($quantity);
In the answer, please indicate if there was any change here between PHP versions.
Bonus: does the static call (::) operator has the same precedence as ->?
$foo->bar(...) is classified in PHP's grammar as a member-call-expression.
This is a form of callable-variable, which itself is a form of variable. Essentially, a call to a member function on an object has the same precedence as any other "raw" variable - $foo->bar(...) and just $foo should be treated identically by the compiler. Neither is an "operator", and so they don't fit in the same hierarchy as the ones listed in the manual page you linked.
For your bonus question, :: is classed as a scoped-call-expression, which holds the same "precedence".
Related
Can somebody explain me, why the strict comparison (===) of two MongoDB\BSON\ObjectIds in PHP returns FALSE although both of ids are type MongoDB\BSON\ObjectId with the same oid?
Next question is about best practice to handle this case. Is it safe to do it via non strict comparison (==) or is there another way to do it e.g. (string)$id1 === (string)$id2?
From the relevant PHP documentation:
When using the identity operator (===), object variables are identical if and only if they refer to the same instance of the same class.
So you should just use the standard comparison operator (==). No string casting required.
Per #jh1711:
BSON\ObjectId ... implements a custom object_compare handler. But the handler just compares the ids
Class Test {
private static $one = ['a','b'];
private static $two = Test::$one; // Throws an error
// Error : syntax error, unexpected '$one', expecting 'identifier' or 'class'
}
Why does it throw an error and what is the way here to make $two = $one?
It is a limitation of the PHP compiler and it is explained in the documentation:
Like any other PHP static variable, static properties may only be initialized using a literal or constant before PHP 5.6; expressions are not allowed. In PHP 5.6 and later, the same rules apply as const expressions: some limited expressions are possible, provided they can be evaluated at compile time.
The key statement here is: "provided they can be evaluated at compile time".
From the error message you receive I can tell you are using PHP 5. On PHP 7 the error message was reworded to clearly state the problem. It says "Constant expression contains invalid operations".
The declaration of the first static variable ($one) compiles because you initialize it with a constant expression. ['a','b'] is an array of strings, it can be evaluated at the compile time, everything is fine.
The second static variable ($two) is initialized with a non-constant expression (Test::$one). Test::$one is a variable. You can tell that its value initial value is known at the compile time (see the paragraph above) and the expression can be evaluated at the compile time.
This kind of behaviour requires a deeper analysis of the code at the compile time. It is probably implemented in C++ or Java compilers but these are languages that are compiled only once and the code they generate is stored in a file and executed or interpreted later. The PHP compiler doesn't work this way for a reason. It compiles the script before each execution, that's why it aims to complete the compilation as fast as possible and doesn't put much effort in code analysis and optimizations.
Update:
As #deceze specifies in a comment, the expression Test::$one cannot be evaluated in the declaration of $two because it uses class Test that is not completely defined at this point. Even the compilers of other languages that allow this kind of reference cannot compute the value of Test::$one when they reach the declaration of $two. They need to use a second compilation pass to be able to evaluate it.
I noticed that in PHP extract(some_function()); will work just like:
$stuff = some_function();
extract($stuff);
But in the PHP's documentation the extract function argument has the & thingy in front, and from what I know that means you have to pass a variable to it.
If the documentation was right, this would produce a strict standards message:
PHP Strict standards: Only variables should be passed by reference
So I think you just found a bug in the documentation. Congratulations.
EDIT
It still doesn't complain if you use it with EXTR_REFS as a second argument:
~❯ php -a
Interactive shell
php > function a(){return array('pwet'=> 42);}
php > extract(a(), EXTR_REFS);
php > echo $pwet;
42
Which is strange because referencing variables defined inside a function doesn't make much sense to me. I think the & might have been introduced because of this option, but appears only in the doc and is not enforced in the code.
EDIT
It seems I'm right, I found this comment in ext/standard/array.c (branches 5.3 and 5.4):
/* var_array is passed by ref for the needs of EXTR_REFS (needs to
* work on the original array to create refs to its members)
* simulate pass_by_value if EXTR_REFS is not used */
The ampersand passes a variable by reference so that when it is used in a function, you are manipulating the original object -- not a new variable with the same value. The documentation is telling you that if you pass a variable to the extract function, then the original object can be updated in some fashion by that function.
So, the answer is yes, you need to pass a variable to that function.
The reason $var_array parameter of the extract function is passed by reference (most likely) is from a holdover from older versions of PHP. Newer versions automatically pass arrays by reference.
The extract function creates a variable list from the contents of a (potentially large) array and it is not recommended that data of that type be passed by value.
Long story short, assign your array to a variable and pass it in that way.
I am new to PHP, and I am practicing with some online tutorials. I usually encounter the sign :: while going through books, online tutorial or blogs. Even if I check some PHP demo applications I see this operator. I tried to put this sign in Google, but I got unexpected results. I even tried to search it in other forums as well, but I didn't got the right answer. I usually call it a bubble colon, but what is its technical name?
:: is the scope resolution operator (you may sometimes find references to Paamayim Nekudotayim, hebrew for "double colon"). It is used to call static functions of a class, as in
class MyClass {
public static function hi() {
echo "hello, world";
}
}
MyClass::hi();
For more details on classes and objects, refer to the official documentation.
It has the exotic name "Paamayim Nekudotayim", but you may just call it Scope Resolution Operator.
It's called scope resolution operator. More information in Scope Resolution Operator (::) (PHP manual).
A single one is called a colon, so you could search for "php double colon" and would find this:
http://php.net/manual/en/keyword.paamayim-nekudotayim.php
Scope Resolution Operator (::)
Sometimes it is useful to refer to functions and variables in base classes or to refer to functions in classes that have not yet any instances. The :: operator is being used for this.
Why don't the function handling functions like call_user_func() support passing parameters by reference?
The docs say terse things like "Note that the parameters for call_user_func() are not passed by reference." I assume the PHP devs had some kind of reason for disabling that capability in this case.
Were they facing a technical limitation? Was it a language design choice? How did this come about?
EDIT:
In order to clarify this, here is an example.
<?php
function more(&$var){ $var++; }
$count = 0;
print "The count is $count.\n";
more($count);
print "The count is $count.\n";
call_user_func('more', $count);
print "The count is $count.\n";
// Output:
// The count is 0.
// The count is 1.
// The count is 1.
This is functioning normally; call_user_func does not pass $count by reference, even though more() declared it as a referenced variable. The call_user_func documentation clearly says that this is the way it's supposed to work.
I am well aware that I can get the effect I need by using call_user_func_array('more', array(&$count)).
The question is: why was call_user_func designed to work this way? The passing by reference documentation says that "Function definitions alone are enough to correctly pass the argument by reference." The behavior of call_user_func is an exception to that. Why?
The answer is embedded deep down in the way references work in PHP's model - not necessarily the implementation, because that can vary a lot, particularly in the 5.x versions. I'm sure you've heard the lines, they're not like C pointers, or C++ references, etc etc... Basically when a variable is assigned or bound, it can happen in two ways - either by value (in which case the new variable is bound to a new 'box' containing a copy of the old value), or by reference (in which case the new variable is bound to the same value box as the old value). This is true whether we're talking about variables, or function arguments, or cells in arrays.
Things start to get a bit hairy when you start passing references into functions - obviously the intent is to be able to modify the original variables. Quite some time ago, call-time pass-by-reference (the ability to pass a reference into a function that wasn't expecting one) got deprecated, because a function that wasn't aware it was dealing with a reference might 'accidentally' modify the input. Taking it to another level, if that function calls a second function, that itself wasn't expecting a reference... then everything ends up getting disconnected. It might work, but it's not guaranteed, and may break in some PHP version.
This is where call_user_func() comes in. Suppose you pass a reference into it (and get the associated the call-time pass-by-reference warning). Then your reference gets bound to a new variable - the parameters of call_user_func() itself. Then when your target function is called, its parameters are not bound where you expect. They're not bound to the original parameters at all. They're bound to the local variables that are in the call_user_func() declaration. call_user_func_array() requires caution too. Putting a reference in an array cell could be trouble - since PHP passes that array with "copy-on-write" semantics, you can't be sure if the array won't get modified underneath you, and the copy won't get detached from the original reference.
The most insightful explanation I've seen (which helped me get my head around references) was in a comment on the PHP 'passing by reference' manual:
http://ca.php.net/manual/en/language.references.pass.php#99549
Basically the logic goes like this. How would you write your own version of call_user_func() ? - and then explain how that breaks with references, and how it fails when you avoid call-time pass-by-reference. In other words, the right way to call functions (specify the value, and let PHP decide from the function declaration whether to pass value or reference) isn't going to work when you use call_user_func() - you're calling two functions deep, the first by value, and the second by reference to the values in the first.
Get your head around this, and you'll have a much deeper understanding of PHP references (and a much greater motivation to steer clear if you can).
See this:
http://hakre.wordpress.com/2011/03/09/call_user_func_array-php-5-3-and-passing-by-reference/
Is it possible to pass parameters by reference using call_user_func_array()?
http://bugs.php.net/bug.php?id=17309&edit=1
Passing references in an array works correctly.
Updated Answer:
You can use:
call_user_func('more', &$count)
to achieve the same effect as:
call_user_func_array('more', array(&$count))
For this reason I believe (unfoundedly) that call_user_func is just a compiler time short cut. (i.e. it gets replaced with the later at compile time)
To give my view on you actual question "Why was call_user_func designed to work this way?":
It probably falls under the same lines as "Why is some methods strstr and other str_replace?, why is array functions haystack, needle and string functions needle, haystack?
Its because PHP was designed, by many different people, over a long period of time, and with no strict standards in place at the time.
Original Answer:
You must make sure you set the variable inside the array to a reference as well.
Try this and take note of the array(&$t) part:
function test(&$t) {
$t++;
echo '$t is '.$t.' inside function'.PHP_EOL;
}
$t = 0;
echo '$t is '.$t.' in global scope'.PHP_EOL;
test($t);
$t++;
echo '$t is '.$t.' in global scope'.PHP_EOL;
call_user_func_array('test', array(&$t));
$t++;
echo '$t is '.$t.' in global scope'.PHP_EOL;
Should output:
$t is 0 in global scope
$t is 1 inside function
$t is 2 in global scope
$t is 3 inside function
$t is 4 in global scope
Another possible way - the by-reference syntax stays the 'right' way:
$data = 'some data';
$func = 'more';
$func($more);
function more(&$data) {
// Do something with $data here...
}