When I reading the code of CodeIgniter,I found some functions written as follows:
function &get_instance()
{
global $CI, $OBJ;
if (is_object($CI))
{
return $CI;
}
return $OBJ->load;
}
I can understand variable refrence,but I can hardly get this through.Is it necessary to use this function style?And any benefit?
Thanks.
In PHP's syntax, this means that the function returns a reference instead of a value. For example:
<?php
$foo = 'foo';
function & get_foo_ref ()
{
global $foo;
return $foo;
}
// Get the reference to variable $foo stored into $bar
$bar = & get_foo_ref();
$bar = 'bar';
echo $foo; // Outputs 'bar', since $bar references to $foo.
?>
In the above example, removing the & from the function decleration would make the $foo variable still contain 'foo', since only the value, not the reference was returned from the function.
This was used more often in PHP4, because it did not pass objects by their reference and cloned them instead. Because of this, object variables had to be passed by reference to avoid unwanted cloning. This is no longer the case in PHP5 and references should not be used for this purpose.
However, functions that return references are not completely useless either (or bad practice, when not used for replacing object references). For example, personally I've used them when creating a script that passes a "path" to an function, which returns reference to variable in that path allowing me to set value to it and read the value. Due to recursive nature of the function, returning the reference was needed.
It is the same concept as variable reference, in fact this "style" is required when you want to return a REFERENCE to a variable and not the value itself.
Explained in the manual here: http://www.php.net/manual/en/functions.returning-values.php
no, there's no benefit. It was necessary for php4, but in php5 it's a bad practice.
Related
Is it considered wrong to pass a variable to a function where the variable passed has the same name as the variable in the function itself? I never see anyone do this, but wondered if there was a reason why (other than readability). Example:
$variable = "value";
function myFunction($variable) {
return $variable;
}
No there is not problem with doing this except some naming confusion. Because,
$variable = "value"; // this declaration belong to a global scope
// but $variable passing through as argument, makes it scope within that function declaration only
function myFunction($variable) {
return $variable;
}
You might have expect that if you call the function like myFunction() i.e. without any parameter, it will return you "value", but absolutely not, cause $variable passed as argument is not the global one.
But if you try to use the global $variable within the myFunction, then it will cause some conflict, that is why no one do this.
Aside from making variables a bit confusing to track, there is nothing wrong with this. They exist in different scopes.
It is up to you but vice versa, if you want to pass intentionally a variable into a function.. then you have to use this:
$variable = "value";
function myFunction($variable) {
global $variable;
return $variable;
}
echo myFunction('another value');
One more point. When creating functions you may set a default value for your params to avoid errors:
function myFunction($variable="default value") {
return $variable;
}
echo myFunction();
There is nothing wrong with it at all, and I would have thought it's quite common.
However, you should always be choosing the best name for a variable, or a parameter, in each situation, not applying one rule to your whole code.
For instance, in a database lookup function, it might be obvious what $id refers to, so it would be a sensible parameter name; but where you are calling it, you will likely have lots of IDs of various sorts, so a more descriptive variable name should be used. On the other hand, each time you build an SQL string, it's probably the only such variable in scope, so giving it a consistent name keeps your code tidy and easy to read.
A common observation is that all code is read more often than it is written (yes, even your personal website that you wouldn't dare share with the world). Variable names should be chosen with that in mind: what makes this code easy to read, understand, and maintain?
You can use global and add default value in param variable.
$variable = "value";
function myFunction($variable = '') {
global $variable;
return $variable;
}
This is just out of curiosity, but I was wondering if there was a way to query all the variables defined inside the scope of a function (exclusively within this scope, and from within that function) and put them in an associative array. Something like an extended get_defined_vars function.
The reason is that it would be nice to be able to save the 'state' of an execution at any point in the program, for instance to debug, log, handle exceptions, or even pass the entire scope of a function to another one. If I'm not mistaken, I think get_object_vars allows doing this with objects.
From the comments of PHP.net
// The very top of your php script
$vars = get_defined_vars();
// Now do your stuff
$foo = 'foo';
$bar = 'bar';
// Get all the variables defined in current scope
$vars = array_diff(get_defined_vars(),$vars);
echo '<pre>';
print_r($vars);
echo '</pre>';
PHP doesn't really have scopes, they were added later, that's why you need $this and global to access a global variable. Objects are implemented basically as arrays because they came later, that's why equality tests on two objects work member by member.
You can try var_dump of course.
Ignore the var_dump bit, you've found get_defined_vars and get_object_vars they are as close as you will get I'm sorry to say.
Another difficulty in telling you about what's in a function is how it does variables, remember PHP is a dynamic language if we have:
$x = "hello";
$$x = "world"
we now have a variable called $hello with value "world", if the $x variable came from some complicated nondeterministic code, it'd be impossible to tell without running. So you can only inspect the current variables, when you're in it, and given how scopes are implemented, get_defined_vars is as close as you will get sorry.
Take the following code from CodeIgniter's show_error function:
$_error =& load_class('Exceptions', 'core');
The documentation for the load_class function says it acts as a singleton. The function basically takes the given parameters and searches for a class in the appropriate path. It then includes the file if it exists. The function is declared as:
function &load_class(...)
Why does it have the & and what is its purpose? Is $_error declared as such as a result of defining the function like that?
I don't see any point of declaring and using load_class like that. From the source code of load_class(), we can see that it caches loaded objects in an array with the class name as the key. If it is not in the cache, it loads an object given a name, and then stores that object reference into the array. In both cases, it returns the element of the array (by reference).
Returning by reference allows the caller to have a reference to the element of the array. The only things that this allows us to do are:
See later changes to that array element (i.e. the value associated with that key) from the outside reference we have. But this is not applicable, since the load_class function never changes the value associated with a key after it sets it.
Have external code be able to change the element in the array, without the load_class function knowing about it. But this would be a highly dubious practice, to mess with the cache from the outside, and I highly doubt this is something the authors wanted.
So there is no legitimate reason to return by reference. My guess is that it is a leftover from PHP 4, when objects were values, and so assigning or returning an "object value" would copy it. In PHP 5, objects are not values; you can only manipulate them through object references, and assigning or returning an object reference by value never copies the object it points to.
The php documentation seems to explain why you have to uses =& even though the function is marked to return a refrence function &load_class
Returning References
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. To return references, use
this syntax:
<?php class foo {
public $value = 42;
public function &getValue() {
return $this->value;
}
}
$obj = new foo;
$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
$obj->value = 2;
echo $myValue;
// prints the new value of $obj->value, i.e. 2. ?>
In this example,
the property of the object returned by the getValue function would be
set, not the copy, as it would be without using reference syntax.
Note: Unlike parameter passing, here you have to use & in both places
- to indicate that you want to return by reference, not a copy, and to indicate that reference binding, rather than usual assignment, should
be done for $myValue.
If you are asking what references in general are the documentation explains.
Sorry for such a lame title, but I just had no idea what to put there, hope you understand it. Plus, I have no idea if similar question has been asked before, because I don't know the proper keywords for it - therefore couldn't google it too.
Basicly... When looking at preg_match_all();, they got this matches parameter that will create new array defined in function, and give us the ability to access it after function execution.
And the question is.. How can I implement such a feature in my own function? But that it could create single variable and/or array.
Thanks in advance!
preg_match_all() accepts a reference to an array, which in its own scope is called $matches. As seen in the function prototype:
array &$matches
If you call the function and pass in a variable, if it does not already exist in the calling scope it will be created. So in your user-defined function, you accept a parameter by reference using &, then work with it inside your function. Create your outer-scope variable by simply declaring it in your function call, like you the way you call preg_match_all() with $matches.
An example:
function foo(&$bar) {
$bar = 'baz';
}
// Declare a variable and pass it to foo()
foo($variable);
echo $variable; // baz
I think you are referring to function parameters passed by reference, are you not?
function putValInVar(&$myVar, $myVal){
$myVar = $myVal;
}
$myVar = 1;
putValInVar($myVar, 2);
echo $myVar; // outputs '2', but will output '1' if we remove the '&' //
By default function arguments in PHP are passed by value. This means that new variables are created at each function call and those variables will exist only inside the function, not affecting anything outside it.
To specify that an argument should be used by reference the syntax is to append an & before declaring it in the function header. This will instruct PHP to use the passed variable inside the function rather than creating a copy of it.
Exception: Objects are always passed by reference. (Well... Not really, but it's complicated. See the comment thread for more info.)
I think what you are asking for is passing-by-reference. What preg_match_all basically does to "create" an array variable outside its scope is:
function preg_match_all($foo, $bar, & $new_var) {
$new_var = array(1,2,3);
}
The crucial point here is & in the function definition. This allows you to overwrite variables in the outer scope when passed.
Stylistically this should be used with care. Try to return arrays or results instead of doing it via reference passing.
Like this:
$myvariable = runfunction();
function runfunction() {
//do some code assign result to variable (ie $result)
return $result;
}
Or
global $result;
function runfunction() {
global $result;
$result = 'something';
}
Consider:
public function & get($name, $default = null)
Why &?
In PHP's syntax, this means that the function returns a reference instead of a value. For example:
<?php
$foo = 'foo';
function & get_foo_ref ()
{
global $foo;
return $foo;
}
// Get the reference to variable $foo stored into $bar
$bar = & get_foo_ref();
$bar = 'bar';
echo $foo; // Outputs 'bar', since $bar references to $foo.
?>
In the above example, removing the & from the function declaration would make the $foo variable still contain 'foo', since only the value, not the reference was returned from the function.
This was used more often in PHP4, because it did not pass objects by their reference and cloned them instead. Because of this, object variables had to be passed by reference to avoid unwanted cloning. This is no longer the case in PHP5 and references should not be used for this purpose.
However, functions that return references are not completely useless either (or bad practice, when not used for replacing object references).
For example, personally I've used them when creating a script that passes a "path" to an function, which returns reference to variable in that path allowing me to set value to it and read the value. Due to the recursive nature of the function, returning the reference was needed.
By rithiur
It means it returns a reference to the result as opposed to a copy of it.
Returning References in PHP
PHP's pass by reference metacharacter. See Passing by Reference