Issue with modifying array by reference - php

In PHP, I have the following code (whittled down, to make it easier to read):
class Var {
public $arr;
function __construct($arr) {
$this->arr = $arr;
}
function set($k, $v) {
$this->arr[$k] = $v;
}
}
class Session extends Var {
function __construct() {}
function init() {
session_start();
parent::__construct($_SESSION);
}
}
$s = new Session();
$s->init();
$s->set('foo', 'bar');
var_dump($_SESSION);
At this point, I want $_SESSION to contain 'foo' => 'bar'. However, the $_SESSION variable is completely empty. Why is this the case? How can I store the $_SESSION variable as a property in order to later modify it by reference?
I have tried replacing __construct($arr) with __construct(&$arr), but that did not work.

You needed to take care of reference on every variable re-assignment you have.
So the first place is __construct(&$arr)
The second is $this->arr = &$arr;
Then it should work.
If you didn't put the & in the latter case - it would "make a copy" for the reference you passed in constructor and assign "a copy" to the $this->arr.
PS: It's really weird to call parent constructor from non-constructor method

Related

Query array or object property?

I'm still new to OOP and this is probably a simple question, not sure if I'm overthinking this.
Let's say we have a simple class like the following that we can use to instantiate an object that can generate an array:
class gen_arr {
public $arr = array();
public function fill_arr() {
$this->arr["key"] = "value";
}
}
// instantiate object from gen_arr
$obj = new gen_arr();
Now if you wanted to get the value of the object's array's item, would you generate an array first and then echo the value like:
$arr = $obj->fill_arr();
echo $arr["key"];
Or would you access the object's property directly?
echo $obj->arr["key"]
In the actual code the property is private and there is a method that allows the viewing of the property array, the above is just to simplify the question.
Are there performance considerations and/or just best practices when it comes to this kind of case?
UPDATE:
It's still unclear from the answers if the best way is to generate an array from the property and access that array or just access the property directly (through the getter method)
Since you are filling the array with items only on fill_arr, those items wont be availabl until you call $arr = $obj->fill_arr();.
If you want to directly call the array, then you have to fill this array on the constructor function of this call like this:
class gen_arr {
public $arr = array();
function __construct() {
$this->arr["key"] = "value";
}
}
First off, the class you shared with us has a range of problems:
its sole instance property is public and can be modified by anyone
you have some temporal coupling, the method fill_arr() needs to be invoked before accessing the the value makes any sense
Encapsulation
Reduce the visibility of the instance property from public to private, so that the property can only be modified by the object itself, and provide an accessor instead:
class gen_arr
{
private $arr;
public function fill_arr()
{
$this->arr["key"] = "value";
}
public function arr()
{
return $this->arr;
}
}
Temporal Coupling
Remove the method fill_arr() and instead initialize the property $arr in one of the following options:
initialize field lazily when accessed the first time
initialize field in the constructor
initialize field with a default value
initialize field with a value injected via constructor
Initialize field lazily when accessed the first time
Initialize the field when it's accessed the first time:
class gen_arr
{
private $arr;
public function arr()
{
if (null === $this->arr) {
$this->arr = [
'key' => 'value',
];
}
return $this->arr;
}
}
Initialize field in the constructor
Assign a value during construction:
class gen_arr
{
private $arr;
public function __construct()
{
$this->arr = [
'key' => 'value',
];
}
public function arr()
{
return $this->arr;
}
}
Initialize field with a default value
Assign a value to the field directly, which works fine if you don't need to do any computation:
class gen_arr
{
private $arr = [
'key' => 'value',
];
public function arr()
{
return $this->arr;
}
}
Initialize field with a value injected via constructor
If the values are not hard-coded or otherwise calculated (as in the previous examples), and you need to be able to instantiate objects with different values, inject values via constructor:
class gen_arr
{
private $arr;
public function __construct(array $arr)
{
$this->arr = $arr;
}
public function arr()
{
return $this->arr;
}
}
Accessing and dereferencing values
This seems like this is your actual question, so the answer is - of course - It depends!.
Let's assume we have provided an accessor instead of accessing the otherwise public field directly:
Since PHP 5.4, the following is possible:
$object = new gen_arr();
echo $object->arr()['key'];
If you are still using an older version of PHP, you obviously can't do that and have to do something like this instead:
$object = new gen_arr();
$arr = $object->arr();
echo $arr['key'];
Largely, though, the answer to this question depends on the circumstances, and what you want to achieve. After all, readability is key for maintenance, so it might just make sense for you to introduce an explaining variable.
Note About your example, you could just use an ArrayObject instead:
$arr = new \ArrayObject([
'key' => 'value',
]);
echo $arr['key']);
For reference, see:
http://wiki.c2.com/?EncapsulationDefinition
http://blog.ploeh.dk/2011/05/24/DesignSmellTemporalCoupling/
http://php.net/manual/en/language.oop5.properties.php
http://wiki.c2.com/?ItDepends
http://php.net/manual/en/migration54.new-features.php
https://refactoring.com/catalog/extractVariable.html
http://wiki.c2.com/?IntroduceExplainingVariable
http://php.net/manual/en/class.arrayobject.php
For an example, see:
https://3v4l.org/qVVBM
First fill up the array
$gen_arr = new gen_arr();
$gen_arr->fill_arr();
then get the values with a getter method
$val = $gen_arr->getValue($key);
A getter method would be like this
public function getValue($key) {
return $this->arr[$key];
}
And certailny make the $arr property private

Is there a way to use a 'pass by reference' in a class constructor?

I have a class which contains multiple public functions which all interact with the same $_SESSION index/variable. Rather than passing that variable into each function every time they are called I would like to simply pass it into the class constructor and have the functions grab it from $this->.
Example of what I am trying to do:
$_SESSION['test'] = array('foo', 'bar');
class MyClass {
// Pass by reference in ___construct arguments
public function __construct(&$test_var) {
$this->test_var = $test_var;
}
public function unset_foo() {
unset($this->test_var[0]);
}
}
$bar = new MyClass($_SESSION['test']);
$bar->unset_foo();
print_r($_SESSION['test']);
The result should then be:
Array
(
[1] => 'bar'
)
This does not work though.
Is there a way to do this?
You always have to pass by ref:
In this line you are not assigning by reference, but copying the value:
$this->test_var = $test_var;
Just add an & there, so that $this->test_var still holds a reference to $_SESSION['test']:
$this->test_var = &$test_var;

PHP object method doesn't behave as I expect

I can't quite understand why the output of this code is '1'.
My guess is that php is not behaving like most other OO languages that I'm used to, in that the arrays that php uses must not be objects. Changing the array that is returned by the class does not change the array within the class. How would I get the class to return an array which I can edit (and has the same address as the one within the class)?
<?php
class Test
{
public $arr;
public function __construct()
{
$this->arr = array();
}
public function addToArr($i)
{
$this->arr[] = $i;
}
public function getArr()
{
return $this->arr;
}
}
$t = new Test();
$data = 5;
$t->addToArr($data);
$tobj_arr = $t->getArr();
unset($tobj_arr[0]);
$tobj_arr_fresh = $t->getArr();
echo count($tobj_arr_fresh);
?>
EDIT: I expected the output to be 0
You have to return the array by reference. That way, php returns a reference to the array, in stead of a copy.
<?php
class Test
{
public $arr;
public function __construct()
{
$this->arr = array();
}
public function addToArr($i)
{
$this->arr[] = $i;
}
public function & getArr() //Returning by reference here
{
return $this->arr;
}
}
$t = new Test();
$data = 5;
$t->addToArr($data);
$tobj_arr = &$t->getArr(); //Reference binding here
unset($tobj_arr[0]);
$tobj_arr_fresh = $t->getArr();
echo count($tobj_arr_fresh);
?>
This returns 0.
From the returning references subpage:
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
Note that although this gets the job done, question is if it is a good practice. By changing class members outside of the class itself, it can become very difficult to track the application.
Because array are passed by "copy on write" by default, getArr() should return by reference:
public function &getArr()
{
return $this->arr;
}
[snip]
$tobj_arr = &$t->getArr();
For arrays that are object, use ArrayObject. Extending ArrayObject is probably better in your case.
When you unset($tobj_arr[0]); you are passing the return value of the function call, and not the actual property of the object.
When you call the function again, you get a fresh copy of the object's property which has yet to be modified since you added 5 to it.
Since the property itself is public, try changing:
unset($tobj_arr[0]);
To: unset($t->arr[0]);
And see if that gives you the result you are looking for.
You are getting "1" because you are asking PHP how many elements are in the array by using count. Remove count and use print_r($tobj_arr_fresh)

Difference between normal and magic setters and getters

I am using a magic getter/setter class for my session variables, but I don't see any difference between normal setters and getters.
The code:
class session
{
public function __set($name, $value)
{
$_SESSION[$name] = $value;
}
public function __unset($name)
{
unset($_SESSION[$name]);
}
public function __get($name)
{
if(isset($_SESSION[$name]))
{
return $_SESSION[$name];
}
}
}
Now the first thing I noticed is that I have to call $session->_unset('var_name') to remove the variable, nothing 'magical' about that.
Secondly when I try to use $session->some_var this does not work. I can only get the session variable using $_SESSION['some_var'].
I have looked at the PHP manual but the functions look the same as mine.
Am I doing something wrong, or is there not really anything magic about these functions.
First issue, when you call
unset($session->var_name);
It should be the same as calling
$session->_unset('var_name');
Regarding not being able to use __get(); What doesn't work? What does the variable get set to and what warnings are given. Ensure you have set error_reporting() to E_ALL.
It may also be a good idea to check you have called session_start
I thought getters and setters were for variables inside the class?
class SomeClass {
private $someProperty;
function __get($name) {
if($name == 'someProperty') return $this->someProperty;
}
function __set($name, $value) {
if($name == 'someProperty') $this->someProperty = $value;
}
}
$someClass = new SomeClass();
$someClass->someProperty = 'value';
echo $someClass->someProperty;
?
class session { /* ...as posted in the question ... */ }
session_start();
$s = new session;
$s->foo = 123;
$s->bar = 456;
print_r($_SESSION);
unset($s->bar);
print_r($_SESSION);
prints
Array
(
[foo] => 123
[bar] => 456
)
Array
(
[foo] => 123
)
Ok, maybe not "magical". But works as intended.
If that's not what you want please elaborate...
This is my understanding till now about magic function
Please correct me if i am wrong...
$SESSION is an array and not an Object
therefore you can access them using $session['field'] and not $session->field
magic Function allow you to use the function name __fnName before any function as
fnNameNewField($value);
so ,it will be separated into NewField as key and will be sent to __fnName and oprations will be done on this
eg:
setNewId($value) will be sent to __set() with key= new_id and Parameters...

Reset Class Instance Variables via Method

Does anyone know how to reset the instance variables via a class method. Something like this:
class someClass
{
var $var1 = '';
var $var2 = TRUE;
function someMethod()
{
[...]
// this method will alter the class variables
}
function reset()
{
// is it possible to reset all class variables from here?
}
}
$test = new someClass();
$test->someMethod();
echo $test->var1;
$test->reset();
$test->someMethod();
I know I could simply do $test2 = new SomeClass() BUT I am particularly looking for a way to reset the instance (and its variables) via a method.
Is that possible at all???
You can use reflection to achieve this, for instance using get_class_vars:
foreach (get_class_vars(get_class($this)) as $name => $default)
$this -> $name = $default;
This is not entirely robust, it breaks on non-public variables (which get_class_vars does not read) and it will not touch base class variables.
Yes, you could write reset() like:
function reset()
{
$this->var1 = array();
$this->var2 = TRUE;
}
You want to be careful because calling new someClass() will get you an entirely new instance of the class completely unrelated to the original.
this could be easy done;
public function reset()
{
unset($this);
}
Sure, the method itself could assign explicit values to the properties.
public function reset()
{
$this->someString = "original";
$this->someInteger = 0;
}
$this->SetInitialState() from Constructor
Just as another idea, you could have a method that sets the default values itself, and is called from within the constructor. You could then call it at any point later as well.
<?php
class MyClass {
private $var;
function __construct() { $this->setInitialState(); }
function setInitialState() { $this->var = "Hello World"; }
function changeVar($val) { $this->var = $val; }
function showVar() { print $this->var; }
}
$myObj = new MyClass();
$myObj->showVar(); // Show default value
$myObj->changeVar("New Value"); // Changes value
$myObj->showVar(); // Shows new value
$myObj->setInitialState(); // Restores default value
$myObj->showVar(); // Shows restored value
?>

Categories