Difference between normal and magic setters and getters - php

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...

Related

Issue with modifying array by reference

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

Accessing _POST and _GET dynamically using ${$varname}

[FINAL EDIT]
Seems like I've been missing an important Warning contained in Variables variable PHP Manual
http://www.php.net/manual/en/language.variables.variable.php :
Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically.
[ORIGINAL QUESTION]
I've encountered a problem trying to set/get html/server variables $_POST, $_GET, $_SESSION etc.. dynamically using a variable to hold it's name :
// Direct name
${'_GET'}['test'] = '1';
// Variable-holded name
$varname = '_GET';
${$varname}['test'] = '2';
echo "value is " . $_GET['test'];
will output :
value is 1
any idea why?
[EDIT 1]
This is why I want to use it this way :
class Variable {
protected static $source;
public function __get($key) {
// Some validation / var manip needed here
if ( isset( ${self::$source}[$key] ) ) {
return ${self::$source}[$key];
}
}
public function __set($key, $value) {
// Some validation / var manip needed here too
${self::$source}[$key] = $value;
}
}
final class Get extends Variable {
use Singleton;
public static function create() {
parent::$source = "_GET";
}
}
final class Post extends Variable {
use Singleton;
public static function create() {
parent::$source = "_POST";
}
}
final class Session extends Variable {
use Singleton;
public static function create() {
parent::$source = "_SESSION";
}
}
create is called in the singleton constructor when instanciated
[EDIT 2] using PHP 5.4.3
I'm guessing it has something to do with the fact that you shouldn't be assigning values to $_GET like that. Anyhow, this works just fine:
$source = '_GET';
echo ${$source}['test'];
// URL: http://domain.com/thing.php?test=yes
// output: "yes"
edit
Coincidentally, today I went back to update some old code where it looks like I was trying to implement exactly this inside of a class, and it wasn't working. I believe that using the global keyword before attempting to access a superglobal via a variable variable will solve your problem as well.
Class MyExample {
private $method = '_POST';
public function myFunction() {
echo ${$this->method}['index']; //Undefined index warning
global ${$this->method};
echo ${$this->method}['index']; //Expected functionality
}
}
You may be looking for variable variables. Taken from PHP.net:
<?php
$a = 'hello';
?>
<?php
$$a = 'world';
?>
<?php
echo "$a ${$a}";
//returns: hello world
//same as
echo "$a $hello";
?>
EDIT
Another user on php.net had your exact question. Here is his answer.
<?php
function GetInputString($name, $default_value = "", $format = "GPCS")
{
//order of retrieve default GPCS (get, post, cookie, session);
$format_defines = array (
'G'=>'_GET',
'P'=>'_POST',
'C'=>'_COOKIE',
'S'=>'_SESSION',
'R'=>'_REQUEST',
'F'=>'_FILES',
);
preg_match_all("/[G|P|C|S|R|F]/", $format, $matches); //splitting to globals order
foreach ($matches[0] as $k=>$glb)
{
if ( isset ($GLOBALS[$format_defines[$glb]][$name]))
{
return $GLOBALS[$format_defines[$glb]][$name];
}
}
return $default_value;
}
?>
Why not just use $_REQUEST which includes $_GET, $_POST and $_COOKIE? Or am I misunderstanding the purpose?

PHP - Indirect modification of overloaded property

I know this question has been asked several times, but none of them have a real answer for a workaround. Maybe there's one for my specific case.
I'm building a mapper class which uses the magic method __get() to lazy load other objects. It looks something like this:
public function __get ( $index )
{
if ( isset ($this->vars[$index]) )
{
return $this->vars[$index];
}
// $index = 'role';
$obj = $this->createNewObject ( $index );
return $obj;
}
In my code I do:
$user = createObject('user');
$user->role->rolename;
This works so far. The User object doesn't have a property called 'role', so it uses the magic __get() method to create that object and it returns its property from the 'role' object.
But when i try to modify the 'rolename':
$user = createUser();
$user->role->rolename = 'Test';
Then it gives me the following error:
Notice: Indirect modification of overloaded property has no effect
Not sure if this is still some bug in PHP or if it's "expected behaviour", but in any case it doesn't work the way I want. This is really a show stopper for me... Because how on earth am I able to change the properties of the lazy loaded objects??
EDIT:
The actual problem only seems to occur when I return an array which contains multiple objects.
I've added an example piece of code which reproduces the problem:
http://codepad.org/T1iPZm9t
You should really run this in your PHP environment the really see the 'error'. But there is something really interesting going on here.
I try to change the property of an object, which gives me the notice 'cant change overloaded property'. But if I echo the property after that I see that it actually DID change the value... Really weird...
All you need to do is add "&" in front of your __get function to pass it as reference:
public function &__get ( $index )
Struggled with this one for a while.
Nice you gave me something to play around with
Run
class Sample extends Creator {
}
$a = new Sample ();
$a->role->rolename = 'test';
echo $a->role->rolename , PHP_EOL;
$a->role->rolename->am->love->php = 'w00';
echo $a->role->rolename , PHP_EOL;
echo $a->role->rolename->am->love->php , PHP_EOL;
Output
test
test
w00
Class Used
abstract class Creator {
public function __get($name) {
if (! isset ( $this->{$name} )) {
$this->{$name} = new Value ( $name, null );
}
return $this->{$name};
}
public function __set($name, $value) {
$this->{$name} = new Value ( $name, $value );
}
}
class Value extends Creator {
private $name;
private $value;
function __construct($name, $value) {
$this->name = $name;
$this->value = $value;
}
function __toString()
{
return (string) $this->value ;
}
}
Edit : New Array Support as requested
class Sample extends Creator {
}
$a = new Sample ();
$a->role = array (
"A",
"B",
"C"
);
$a->role[0]->nice = "OK" ;
print ($a->role[0]->nice . PHP_EOL);
$a->role[1]->nice->ok = array("foo","bar","die");
print ($a->role[1]->nice->ok[2] . PHP_EOL);
$a->role[2]->nice->raw = new stdClass();
$a->role[2]->nice->raw->name = "baba" ;
print ($a->role[2]->nice->raw->name. PHP_EOL);
Output
Ok die baba
Modified Class
abstract class Creator {
public function __get($name) {
if (! isset ( $this->{$name} )) {
$this->{$name} = new Value ( $name, null );
}
return $this->{$name};
}
public function __set($name, $value) {
if (is_array ( $value )) {
array_walk ( $value, function (&$item, $key) {
$item = new Value ( $key, $item );
} );
}
$this->{$name} = $value;
}
}
class Value {
private $name ;
function __construct($name, $value) {
$this->{$name} = $value;
$this->name = $value ;
}
public function __get($name) {
if (! isset ( $this->{$name} )) {
$this->{$name} = new Value ( $name, null );
}
if ($name == $this->name) {
return $this->value;
}
return $this->{$name};
}
public function __set($name, $value) {
if (is_array ( $value )) {
array_walk ( $value, function (&$item, $key) {
$item = new Value ( $key, $item );
} );
}
$this->{$name} = $value;
}
public function __toString() {
return (string) $this->name ;
}
}
I've had this same error, without your whole code it is difficult to pinpoint exactly how to fix it but it is caused by not having a __set function.
The way that I have gotten around it in the past is I have done things like this:
$user = createUser();
$role = $user->role;
$role->rolename = 'Test';
now if you do this:
echo $user->role->rolename;
you should see 'Test'
Though I am very late in this discussion, I thought this may be useful for some one in future.
I had faced similar situation. The easiest workaround for those who doesn't mind unsetting and resetting the variable is to do so. I am pretty sure the reason why this is not working is clear from the other answers and from the php.net manual. The simplest workaround worked for me is
Assumption:
$object is the object with overloaded __get and __set from the base class, which I am not in the freedom to modify.
shippingData is the array I want to modify a field of for e.g. :- phone_number
// First store the array in a local variable.
$tempShippingData = $object->shippingData;
unset($object->shippingData);
$tempShippingData['phone_number'] = '888-666-0000' // what ever the value you want to set
$object->shippingData = $tempShippingData; // this will again call the __set and set the array variable
unset($tempShippingData);
Note: this solution is one of the quick workaround possible to solve the problem and get the variable copied. If the array is too humungous, it may be good to force rewrite the __get method to return a reference rather expensive copying of big arrays.
I was receiving this notice for doing this:
$var = reset($myClass->my_magic_property);
This fixed it:
$tmp = $myClass->my_magic_property;
$var = reset($tmp);
I agree with VinnyD that what you need to do is add "&" in front of your __get function, as to make it to return the needed result as a reference:
public function &__get ( $propertyname )
But be aware of two things:
1) You should also do
return &$something;
or you might still be returning a value and not a reference...
2) Remember that in any case that __get returns a reference this also means that the corresponding __set will NEVER be called; this is because php resolves this by using the reference returned by __get, which is called instead!
So:
$var = $object->NonExistentArrayProperty;
means __get is called and, since __get has &__get and return &$something, $var is now, as intended, a reference to the overloaded property...
$object->NonExistentArrayProperty = array();
works as expected and __set is called as expected...
But:
$object->NonExistentArrayProperty[] = $value;
or
$object->NonExistentArrayProperty["index"] = $value;
works as expected in the sense that the element will be correctly added or modified in the overloaded array property, BUT __set WILL NOT BE CALLED: __get will be called instead!
These two calls would NOT work if not using &__get and return &$something, but while they do work in this way, they NEVER call __set, but always call __get.
This is why I decided to return a reference
return &$something;
when $something is an array(), or when the overloaded property has no special setter method, and instead return a value
return $something;
when $something is NOT an array or has a special setter function.
In any case, this was quite tricky to understand properly for me! :)
This is occurring due to how PHP treats overloaded properties in that they are not modifiable or passed by reference.
See the manual for more information regarding overloading.
To work around this problem you can either use a __set function or create a createObject method.
Below is a __get and __set that provides a workaround to a similar situation to yours, you can simply modify the __set to suite your needs.
Note the __get never actually returns a variable. and rather once you have set a variable in your object it no longer is overloaded.
/**
* Get a variable in the event.
*
* #param mixed $key Variable name.
*
* #return mixed|null
*/
public function __get($key)
{
throw new \LogicException(sprintf(
"Call to undefined event property %s",
$key
));
}
/**
* Set a variable in the event.
*
* #param string $key Name of variable
*
* #param mixed $value Value to variable
*
* #return boolean True
*/
public function __set($key, $value)
{
if (stripos($key, '_') === 0 && isset($this->$key)) {
throw new \LogicException(sprintf(
"%s is a read-only event property",
$key
));
}
$this->$key = $value;
return true;
}
Which will allow for:
$object = new obj();
$object->a = array();
$object->a[] = "b";
$object->v = new obj();
$object->v->a = "b";
I have run into the same problem as w00, but I didn't had the freedom to rewrite the base functionality of the component in which this problem (E_NOTICE) occured. I've been able to fix the issue using an ArrayObject in stead of the basic type array(). This will return an object, which will defaulty be returned by reference.

How to reuse an object if it existed

I'm trying to determine whether or not a given object has been created. I see there are methods for class_exists and method_exists but what I'm trying to figure out is if new Foo() has been called (and hopefully figure out what variable it was assigned to, but that is not as important).
If I understand you correctly you are trying to initialize object only once. If this is the case why not to use singleton pattern? This will free you from checking of existence of object:
class MyClass {
private static $instance;
private function __construct() {}
public static function getInstance() {
if (empty(self::$instance)) {
self::$instance = new __CLASS__();
}
return self::$instance;
}
}
You can use this code like this:
$obj = MyClass::getInstance();
With similar approach you can define additional helper static methods which will check whether object was instantiated or not. You just need to keep instance statically inside your class.
Edit: After seeing the reason you are needing this in the comments above, this is definitely not the way to go about it.
Here ya go. It could be optimized a little, but should work fine.
Also, passing get_defined_vars() to the function every time is necessary because that function only retrieves the vars within the scope it's called. Calling it inside the function would only give the vars within the scope of that function.
<?php
function isClassDeclared($class_name, $vars, $return_var_name = FALSE) {
foreach ($vars AS $name => $val) {
if (is_object($val) && $val instanceof $class_name)
return $return_var_name ? $name : TRUE;
}
return FALSE;
}
class Foo {}
$foo = new Foo;
echo '<pre>';
var_dump(isClassDeclared('foo', get_defined_vars(), TRUE));
var_dump(isClassDeclared('bar', get_defined_vars(), TRUE));
echo '</pre>';

Dynamically creating instance variables in PHP classes

I'm not sure if this is a trivial questions but in a PHP class:
MyClass:
class MyClass {
public $var1;
public $var2;
constructor() { ... }
public method1 () {
// Dynamically create an instance variable
$this->var3 = "test"; // Public....?
}
}
Main:
$test = new MyClass();
$test->method1();
echo $test->var3; // Would return "test"
Does this work?? How would I get this to work? Ps. I wrote this quickly so please disregard any errors I made with setting up the class or calling methods!
EDIT
What about making these instance variables that I create private??
EDIT 2
Thanks all for responding - Everyone is right - I should have just tested it out myself, but I had an exam the next morning and had this thought while studying that I wanted to check to see if it worked. People keep suggesting that its bad OOP - maybe but it does allow for some elegant code. Let me explain it a bit and see if you still think so. Here's what I came up with:
//PHP User Model:
class User {
constructor() { ... }
public static find($uid) {
$db->connect(); // Connect to the database
$sql = "SELECT STATEMENT ...WHERE id=$uid LIMIT 1;";
$result = $db->query($sql); // Returns an associative array
$user = new User();
foreach ($result as $key=>$value)
$user->$$key = $value; //Creates a public variable of the key and sets it to value
$db->disconnect();
}
}
//PHP Controller:
function findUser($id) {
$User = User::find($id);
echo $User->name;
echo $User->phone;
//etc...
}
I could have just put it in an associative array but I can never correctly name that array something meaningful (ie. $user->data['name'] ... ugly.) Either way you have to know what is in the database so I do not really understand what the argument is that its confusing, especially since you can just var dump objects for debugging.
Why dont you just write the code and see for yourself?
<?php
class Foo
{
public function __construct()
{
$this->bar = 'baz';
}
}
$foo = new Foo;
echo $foo->bar; // outputs 'baz'
and
var_dump($foo);
gives
object(Foo)#1 (1) {
["bar"] => string(3) "baz"
}
but
$r = new ReflectionObject($foo);
$p = $r->getProperty('bar');
var_dump($p->isPublic());
will throw an Exception about 'bar' being unknown, while
$r = new ReflectionObject($foo);
$p = $r->getProperties();
var_dump($p[0]->isPublic());
will return true.
Now, should you do this type of assignment? Answer is no. This is not good OOP design. Remember, OOP is about encapsulation. So, if bar is describing some public property of the class, make it explicit and declare it in your class as public $bar. If it is supposed to be private declare it as private $bar. Better yet, dont use public properties at all and make them protected and provide access to them only through getters and setters. That will make the interface much more clearer and cleaner as it conveys what interaction is supposed to be possible with an object instance.
Assigning properties on the fly here and there across your code, will make maintaining your code a nightmare. Just imagine somewhere along the lifecylce of Foo someone does this:
$foo = new Foo;
$foo->monkey = 'ugh';
echo $foo->monkey; // outputs 'ugh'
Now, from looking at the class definition above, there is absolutely no way, a developer can see there is now a monkey patched into Foo. This will make debugging a pain, especially if code like this is frequent and distributed across multiple files.
Yes that will indeed work. Auto-created instance variables are given public visibility.
yes that works as you'd hope/expect.
I you wanted to make private variables on the fly you could use php magic functions to emulate this, e.g
MyClass
<?php
class MyClass {
public $var1;
public $var2;
private $data = array();
public function __get($key) {
// for clarity you could throw an exception if isset($this->data[$key])
// returns false as it is entirely possible for null to be a valid return value
return isset($this->data[$key]) ? return $this->data[$key] : null;
}
public function __set($key, $value) {
$this->data[$key] = $value;
}
}
?>
Main
<?php
$test = new MyClass();
$test->myVar = 'myVar is technically private, i suppose';
echo $this->myVar; // 'myVar is technically private
?>
Although these dynamically created variables are technically private, they are infact publicly accessible... i cannot image the purpose for wanting to dynamically create private instance variables. I would question your design.
Did you try it?
It is possible but you might get strict errors. If you dynamically need to create these variables, you are probably doing something wrong.
You should either change this into a function:
function var($no) { .. }
or use __get (http://ca.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members)

Categories