Call-time pass-by-reference has been deprecated [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Call-time pass-by-reference has been deprecated;
My Kohana site, get this alert in libraries file.
Call-time pass-by-reference has been deprecated
Thats problem line:
call_user_func('Formo_'.$name.'::load', & $this);
How can i solve this?

Remove the & before $this.
PHP5 doesn't need that added - all objects are passed as object identifiers by default, no need to mimic this with passing by reference as it was required for PHP 4.

To pass a variable by reference in php5 you need to have & on your function declaration. NOT when you are calling the function.
function call_user_func($param1, &$param2) {
// $param2 will be a reference
// as mentioned by damianb though objects are by default references
// http://php.net/manual/en/language.oop5.references.php
}
when calling this just pass in your params as normal and param2 will be passed by reference.
http://php.net/manual/en/language.references.pass.php
The above link clearely explains the error.
Note: There is no reference sign on a function call - only on function
definitions. Function definitions alone are enough to correctly pass
the argument by reference. As of PHP 5.3.0, you will get a warning
saying that "call-time pass-by-reference" is deprecated when you use &
in foo(&$a);.

Related

Deprecated create_function() call [duplicate]

This question already has answers here:
PHP 7.2 Function create_function() is deprecated
(6 answers)
Closed 3 years ago.
I just updated my server to the latest version of php 7.2 and now I have some depreciation warnings. What should I do?
This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.
Here is my code:
if(!array_key_exists('callable', $this->translation_plural)) {
$this->translation_plural['callable'] = create_function('$n', $this->translation_plural['function']);
}
The documentation recommends using anonymous functions. Given that $this->translation_plural['function'] looks like it is a string, you should consider a rewrite.
If you want to get rid of the warning, you can use the following:
$this->translation_plural['callable'] = function($n) { return eval($this->translation_plural['function']); };
This doesn't help your code at all, you are still using eval() which is bad practise. The documentation warns against using it.
The only difference is, create_function() used it internally, now it is very explicit.

'&' sign before function name in PHP [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
Can you please explain to me the differences between two functions:
function &a(){
return something;
}
and
function b(){
return something;
}
Thanks!
The first returns a reference to something, the second a copy of something.
In first case, when the caller modify the returned value, something will be modified as a global variable do.
In the second case, modifying a copy as no effect to the source.
An ampersand before a function name means the function will return a reference to a variable instead of the value.
According to this LINK
Returning by reference is useful when you want to use a function to find to which
variable a reference should be bound. Do not use return-by-reference to increase
performance. The engine will automatically optimize this on its own. Only return
references when you have a valid technical reason to do so.

Can I use the arrow operator on a new instance of an object? [duplicate]

This question already has answers here:
In PHP, can you instantiate an object and call a method on the same line?
(9 answers)
Closed 9 years ago.
The following PHP code is valid:
$a = new MyClass();
$a->myFunction();
Is there a valid way to combine these two statements? My first attempt understandably results in a syntax error:
new MyClass()->myFunction();
I then added brackets around the created object, and that ran fine on PHP 5.4.17 but not on PHP 5.3.26:
(new MyClass())->myFunction();
At what point is this syntax accepted, or should I abandon this idea and just use two lines. My actual use case is more complicated and it would make the code much neater if I didn't have to keep creating single-use variables all the time.
I am unable to find anything about this in the documentation.
This answer comes directly from this question also asked here on SO.
The feature you have asked for is available from PHP 5.4. Here is the list of new features in PHP 5.4:
http://docs.php.net/manual/en/migration54.new-features.php
And the relevant part from the new features list:
Class member access on instantiation has been added, e.g. (new Foo)->bar().
(new MyClass())->myFunction();
is justfine since PHP 5.4. You can use it, but i think two-line approach is cleaner, backwards compatible with older PHP versions and won't confuse newcomming programmers who read your code :)
Added in PHP 5.4.0. Class member access on instantiation has been added, e.g. (new Foo)->bar().
http://docs.php.net/manual/en/migration54.new-features.php
The (new MyClass())->myFunction() notation is introduced in PHP 5.4 (including some other short-hand notations, see the new features-page).
One workaround (which I don't recommend to misuse this in all situations) is to have a static method in MyClass:
class MyClass {
public static newInstance() {
return new self();
}
public function myFunction() {}
}
MyClass::newInstance()->myFunction();

cakephp PHP 5.4.4 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Declaration of Methods should be Compatible with Parent Methods in PHP
I just installed php 5.4.4. and I all of a sudden get a strict warning.
Does someone know what it is?
Strict (2048): Declaration of User::beforeSave() should be compatible with Model::beforeSave($options = Array) [APP/Model/User.php, line 3]
In APP/Model/User.php, change the declaration to match the class it extends, Model
function beforeSave( array $options ){
...
If you look closely, you'll notice that the methods signature differs. Model::beforeSave() accepts an optional array, whilst your method doesn't accept anything.
However, the message is not severe. It's a strict, meaning that you're breaking standards, but is not like you're on Titanic.

Can I immediately evaluate an anonymous function? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Immediately executing anonymous functions
I want to immediately evaluate an anonymous function rather than it appearing as a Closure object in method args. Is this possible?
For example:
$obj = MyClass;
$obj->Foo(function(){return "bar";}); // passes a Closure into Foo()
$obj->Foo(function(){return "bar";}()); // passes the string "bar" into Foo()?
The 3rd line is illegal syntax -- is there any way to do this?
Thanks
You could do it with call_user_func ... though that might be a bit silly when you could just assign it to a variable and subsequently invoke the variable.
call_user_func(function(){ echo "bar"; });
You might think that PHP 5.4 with it's dereferencing capabilities would make this possible. You'd be wrong, however (as of RC6, anyway).

Categories