getStaticProperties of ReflectionClass returns inherited class properties - php

This is my code:
class A {
public static $a = '1';
}
class B extends A {
public static $b = '2';
}
$refclass = new ReflectionClass('B');
foreach ($refclass->getStaticProperties() as $key => $property)
echo $key;
This code prints $a and $b.
Is there any way to get the class properties without getting the inherited parents class properties.

foreach ($refclass->getStaticProperties() as $key => $property)
if ($refclass->getProperty($key)->getDeclaringClass() == $refclass) {
echo $key;
}
}
Or, perhaps more elegant:
$props = array_filter($refclass->getProperties(ReflectionProperty::IS_STATIC), function ($prop) use ($refclass) {
return $prop->getDeclaringClass() == $refclass;
});

Related

Update values of all public variables of class using get_object_vars()

I have class with 100 public members. How can I update them in an automated way, ie without specifying their name. I have tried this and I'm getting variables but the changes made doesn't reflect on actual object. Please advice.
class foo {
public $b = 1;
public $c = 2;
function __construct()
{
$x = get_object_vars($this);
foreach ($x as $obj) {
$obj = 9;
}
}
}
$test = new foo;
echo $test->c;
It prints vale of 'c' as 2 instead of 9
function __construct()
{
$x = get_object_vars($this);
foreach ($x as $key => $value) {
$this->$key = 9;
}
}

Sharing array inside object through classes in php

My problem is that I have an object shared through two classes that contains an array inside of it and along the script, someone will request some of the classes the value and a foreach loop will change such value and I want this change to affect every reference of the value.
class bar {
protected $obj;
function __construct(&$obj) {
$this->obj = $obj;
}
public function output() {
print_r($this->obj->value);
}
}
class foo {
protected $obj;
function __construct(&$obj) {
$this->obj = $obj;
}
public function val() {
$result = array();
foreach($this->obj->value as $it){
$result[] = $it;
}
return $result;
}
}
// Shared Object
$obj = new stdClass();
// Default value
$obj->value = array('teste', 'banana', 'maca');
// Class 1
$bar = new bar($obj);
// Class 2
$foo = new foo($obj);
// Someone requests from class 2 the values and changes it
$new = $foo->val();
$new[] = 'abc';
// Class 1 outputs the value
$bar->output(); // this will print the default value. I want this to also have 'abc' value.
The main problem, is that you are building a new array at foo:val, you must return the original object to be modified.
I suggest use ArrayObject, have the same behavior of array but is a object, then always is passed by reference.
<?php
class MyArrayObject extends ArrayObject {
public function replace(Array $array)
{
foreach($this->getArrayCopy() as $key => $value) {
$this->offsetUnset($key);
}
foreach ($array as $key => $value) {
$this[$key] = $value;
}
}
}
class bar {
protected $obj;
function __construct(MyArrayObject $obj) {
$this->obj = $obj;
}
public function output() {
print_r($this->obj);
}
}
class foo {
protected $obj;
function __construct(MyArrayObject $obj) {
$this->obj = $obj;
}
public function val() {
$result = array('foo', 'bar');
$this->obj->replace($result);
return $this->obj;
}
}
// Shared Object
$obj = new MyArrayObject(array('teste', 'banana', 'maca'));
// Class 1
$bar = new bar($obj);
// Class 2
$foo = new foo($obj);
// Someone requests from class 2 the values and changes it
$new = $foo->val();
$new[] = 'abc';
// Class 1 outputs the value
$bar->output(); // this will print the default value. I want this to also
var_dump($obj);

Copying an instance of a PHP class while preserving the data in a base class?

I have the following three classes:
class a
{ public $test; }
class b extends a { }
class c extends a
{
function return_instance_of_b() { }
}
As you can see, both classes b and c derive from a. In the return_instance_of_b() function in c, I want to return an instance of the class b. Basically return new b(); with one additional restriction:
I need the data from the base class (a) to be copied into the instance of b that is returned. How would I go about doing that? Perhaps some variant of the clone keyword?
You can use the get_class_vars function to retrieve the names of the variables you want to copy, and just loop to copy them.
The variables that are defined are protected so they are visible to get_class_vars in its scope (since c extends a), but not directly accessible outside the class. You can change them to public, but private will hide those variables from get_class_vars.
<?php
class a
{
protected $var1;
protected $var2;
}
class b extends a
{
}
class c extends a
{
function __construct()
{
$this->var1 = "Test";
$this->var2 = "Data";
}
function return_instance_of_b()
{
$b = new b();
// Note: get_class_vars is scope-dependant - It will not return variables not visible in the current scope
foreach( get_class_vars( 'a') as $name => $value) {
$b->$name = $this->$name;
}
return $b;
}
}
$c = new c();
$b = $c->return_instance_of_b();
var_dump( $b); // $b->var1 = "Test", $b->var2 = "Data
I believe you can achieve this with some reflection. Not very pretty code, I'm sure there is a much more succinct method to achieve this but here you go.
class a
{
public $foo;
public $bar;
function set($key, $value) {
$this->$key = $value;
}
function get($key) {
return $this->$key;
}
}
class b extends a
{
function hello() {
printf('%s | %s', $this->foo, $this->bar);
}
}
class c extends a
{
public $ignored;
function return_instance_of_b() {
$b = new b();
$reflection = new ReflectionClass($this);
$parent = $reflection->getParentClass();
foreach($parent->getProperties() as $property) {
$key = $property->getName();
$value = $property->getValue($this);
$b->$key = $value;
}
return $b;
}
}
$c = new c();
$c->set('foo', 'bar');
$c->set('bar', 'bar2');
$c->set('ignored', 'should be!');
$b = $c->return_instance_of_b();
$b->hello();
// outputs bar | bar2
Additionally you could use nickb's answer but instead of hard coding the class you could use get_parent_class
function return_instance_of_b()
{
$b = new b();
foreach(get_class_vars(get_parent_class(__CLASS__)) as $name => $value) {
$b->$name = $this->$name;
}
return $b;
}

How to write an array to do all the setter things in php?

For example, I have a object like this:
class myObj{
private $a;
private $b;
//getter , setter
}
And I would like to do something like:
$myObj = initWitharray(array('a'=> 'myavalue',
'b'=> 'mybvalue'));
And the myObj will have all the a value and b value. How can I do so ? Thank you.
As NullUserException suggested:
<?php
class myObj {
private $a;
private $b;
public function initWithArray(array $arr) {
foreach ($arr as $k => $v) {
$this->$k = $v;
}
return $this;
}
public function get($name) {
return $this->$name;
}
}
// usage
$myObj = new myObj();
echo $myObj->initWithArray(array(
'a' => 'myavalue',
'b' => 'mybvalue'))
->get('a');
function initWithArray(array $a){
$myObj = new myObj();
foreach($a as $k => $v){
$myObj->$k = $v;
}
return $myObj;
}
class myObj {
private $a;
private $b;
public function __set($name, $value) {
$this->$name = $value;
}
public function __get($name){
if($this->$name != null)
return $this->$name;
return null;
}
}
Or, as said in the comments, it's better if init function would be a member of a class.
Try the following:
class myObj {
private $a;
private $b;
function __construct($passedArray){
$this->a = array_key_exists('a', $passedArray) ? $passedArray['a'] : 'default_value_for_a';
$this->b = array_key_exists('b', $passedArray) ? $passedArray['b'] : 'default_value_for_b';
}
//Rest of the code
}
Then:
newObj = new myObj(array('a'=> 'myavalue', 'b'=> 'mybvalue'))
You could use the class constructor to pass in options when you create a new object. Doing it this way, you should also separate out the setOptions method so you can update the options after init as well.
Use this class like this: (shows both ways to set options)
$object = new myClass(array('a'=>'foo'));
$object->setOptions(array('b'=>'bar'));
Also, try not to confuse object with class. An object is an instance of a class.
class myClass
{
private $a;
private $b;
public function __construct(array $options = null)
{
if (null !== $options) {
$this->setOptions($options);
}
}
public function setOptions(array $options)
{
foreach ($options as $key => $value) {
if (isset($this->$key)) {
$this->$key = $value;
}
}
return $this;
}
}
I usually adopts the approach which gives me total control over the object, like allowing someone to access the property. denying the permission, allowing access to only those which i think is appropriate according to application etc. and that's the purpose of object.
Have a look at the example below.
Example
class MyObj {
private $data = array('one' => null, 'two' => null);
public function __set($property, $value) {
//Only allow to set those properties which is declared in $this->data array
if(array_key_exists($property, $this->data)) {
return $this->data[$property] = $value;
} else {
//if you want to throw some error.
}
}
//you can allow or disallow anyone from accessing the class property directly.
public function __get($property) {
//To deny the access permission, simply throw an error and return false.
$error = 'access denied to class property {' . $property . '}';
return false;
//Or Else Allow permission to access class property
//return $this->data[$property];
}
}
the above example demonstrates on how you can gain more control over the class property, by declaring class property $data as private you are basically disallowing anyone to do any sort of manipulation on the class property directly. whatever operation is to be carried out is done through PHP's getter __get() and setter __set() method. of course you can modify the above code according to your need, you just new a very few line of changes and it will behave the way you want it to.

Working with __get() by reference

With an example class such as this:
class Test{
public function &__get($name){
print_r($name);
}
}
An instance of Test will kick back output as such:
$myTest = new Test;
$myTest->foo['bar']['hello'] = 'world';
//outputs only foo
Is there a way I can get more information about what dimension of the array is being accessed, showing me (from the previous example) that the bar element of foo, and the hello element of bar are being targeted?
You can't with the current implementation. In order for this to work, you will have to create an array object (i.e.: an object that implements ArrayAccess). Something like:
class SuperArray implements ArrayAccess {
protected $_data = array();
protected $_parents = array();
public function __construct(array $data, array $parents = array()) {
$this->_parents = $parents;
foreach ($data as $key => $value) {
if (is_array($value)) {
$value = new SuperArray($value, array_merge($this->_parents, array($key)));
}
$this[$key] = $value;
}
}
public function offsetGet($offset) {
if (!empty($this->_parents)) echo "['".implode("']['", $this->_parents)."']";
echo "['$offset'] is being accessed\n";
return $this->_data[$offset];
}
public function offsetSet($offset, $value) {
if ($offset === '') $this->_data[] = $value;
else $this->_data[$offset] = $value;
}
public function offsetUnset($offset) {
unset($this->_data[$offset]);
}
public function offsetExists($offset) {
return isset($this->_data[$offset]);
}
}
class Test{
protected $foo;
public function __construct() {
$array['bar']['hello'] = 'world';
$this->foo = new SuperArray($array);
}
public function __get($name){
echo $name.' is being accessed.'.PHP_EOL;
return $this->$name;
}
}
$test = new Test;
echo $test->foo['bar']['hello'];
Should output:
foo is being accessed.
['bar'] is being accessed
['bar']['hello'] is being accessed
world
No you can't.
$myTest->foo['bar']['hello'] = 'world'; goes through the following translation
$myTest->__get('foo')['bar']['hello'] = 'world'; breaking them in parts become
$tmp = $myTest->__get('foo')
$tmp['bar']['hello'] = 'world';
What you can do is to create an ArrayAccess Derived Object. Where you define your own offsetSet() and return that from __get()
Instead of returning an array, you could return an object that implements ArrayAccess. Objects are always returned and passed by reference. This pushes the problem at least on level down.

Categories