php how to add a reference - php

i would like to ask how to convert: method(Class1::Class2 &class); in php?
what should i write: me->method(????)
hx. appreciate
I am working in c__/ubuntu.

The same way:
function method(&$variable)
However, if &class is an object, it's not necessary - since PHP 5, objects are automatically passed as reference. a kind of references in themselves, so this isn't necessary.

Related

CodeIgniter - & operator

What exactly does the following line of code doing:
$config = &get_config();
Is & operator passing by reference? Also is get_config() a CodeIgniter helper? I couldn't Google the explanation.
As Konrad Rudolph said here: https://stackoverflow.com/a/3957588/837765
The & operator tells PHP not to copy the array when passing it to the
function. Instead, a reference to the array is passed into the
function, thus the function modifies the original array instead of a
copy.
get_config() loads the main config.php file in an array and you modify the returns array directly with the & operator.
It's not a helper. Take a look here (to find the get_config() function) :
http://www.8tiny.com/source/codeigniter/nav.html?_functions/index.html
Is & operator passing by reference?
Yes, it is passed by reference so that you can change config array.
Also is get_config() a CodeIgniter helper?
No, it's a core CodeIgniter method which loads the config array defined in application/config/config.php.
You can look at the source here.
The & operator is PHP's reference operator.
CodeIgniter is a rather old framework. In older versions of PHP objects used to be passed by value. This meant that PHP was quite wasteful when it came to stuff like memory allocation, which made things slower than they needed to be. To prevent PHP from allocating memory every time you wanted to access an object you would instead use references.
In newer versions of PHP objects and arrays are automatically passed by reference. When it comes to arrays, new memory is only allocated if you change the array.
99 per cent of the time using references is not necessary. PHP will optimize the code for you in a way that makes sense. You should only use references if you understand what they do, and you have a legit reason to use them.
You can find the get_config() function by searching for it in the source code on GitHub.

Create actual copy of variables passed by reference in php

Actually, I have a function where a certain variable is passed as argument by reference. I want to create an actual copy of this variable inside my function instead of having a reference. How can I accomplish this in php?
References in PHP do not work as pointers; actually variables in PHP are zval structures, and they contain information for the ref count, is the variable a reference and so on. This works transparently for you, and all that matters when you are using a reference is that you are modifying the original object, and possibly use less memory.
So, if you want to work with a fresh copy of the variable, to be safe from modifications, you can do:
$new_copy = $copy;
or if $copy is an object:
$new_copy = clone $copy;

Can I extract function return value?

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.

when do we need to create pass/call by reference function

I will always be in confusion whether to create pass/call by reference functions. It would be great if someone could explain when exactly I should use it and some realistic examples.
A common reason for calling by reference (or pointers) in other languages is to save on space - but PHP is smart enough to implement copy-on-write for arguments which are declared as passed-by-value (copies). There are also some hidden semantic oddities - although PHP5 introduced the practice of always passing objects by reference, array values are always stored as references, call_user_func() always calls by value - never by reference (because it itself is a function - not a construct).
But this is additional to the original question asked.
In general its good practice to always declare your code as passing by value (copy) unless you explicitly want the value to be different after the invoked functionality returns. The reason being that you should know how the invoked functionality changes the state of the code you are currently writing. These concepts are generally referred to as isolation and separation of concerns.
Since PHP 5 there is no real reason to pass values by reference.
One exception is if you want to modify arrays in-place. Take for example the sort function. You can see that the array is passed by reference, which means that the array is sorted in place (no new array is returned).
Or consider a recursive function where each call needs to have access to the same datum (which is often an array too).
In php4 it was used for large variables. If you passed an array in a function the array was copied for use in the function, using a lot of memory and cpu. The solution was this:
function foo(&$arr)
{
echo $arr['value'];
}
$arr = new array();
foo($arr);
This way you only passed the reference, a link to the array and save memory and cpu. Since php5 every object and array (not sure of scalars like int) are passed by reference internally so there isn't any need to do it yourself.
This is best when your function will always return a modified version of the variable that is passed to it to the same variable
$var = modify($var);
function modify($var)
{
return $var.'ret';
}
If you will always return to the passed variable, using reference is great.
Also, when dealing with large variables and especially arrays, it is good to pass by reference wherever feasible. This helps save on memory.
Usually, I pass by reference when dealing with arrays since I usually return to the modified array to the original array.

PHP new operator returning reference

I'm working with some old PHP code that has a lot of the following:
$someVar =& new SomeClass();
Did the new operator ever return a value, um, not by reference? (That feels strange to type. I feel like I'm losing my mind.)
It was one of those sort of optimization techniques taught in a lot of older books on OOP in PHP 4.
Basically, the initial object created in memory is one the application can't access unless you return the instance by reference. Otherwise you get a copy of the object - the only catch is that the original exists without a symbol. Kinda dumb.
But ya, object creating and passing and references in PHP 4 is a monumental mess.
Thats PHP4 code.
From the documentation: (now removed)
"new" does not return a reference by
default, instead it returns a copy.
[1]: http://no.php.net/manual/en/language.oop.newref.php
See also my answer here which includes a simple code sample to illustrate the issue.

Categories