class A {
protected $a = 'aaa';
}
class B extends A {
protected $a = 'bbb';
public function __construct(){
echo parent::$a; // Fatal error: Access to undeclared static property: A::$a in main.php on line 11
}
}
$b = new B();
I want to access $a variable from class A in constructor of class B. Be aware that $a variable is overwritten in class B. How can I access parent::$a?
The only way to do this would be to declare $a as static:
protected static $a = 'aaa';
But that will make the value of parent::$a the same for all instances. If you want separate values, this cannot be done, and you'd be better off renaming the variables, eg one is $a and the other is $b.
class A {
protected $a = 'aaa';
}
class B extends A {
protected $a = 'bbb';
public function __construct(){
echo parent::$a; // Fatal error: ...
}
}
$b = new B();
How can I access parent::$a?
You cant, parent::$a means you are trying to access a static property from a parent class.
instead of doing this,use the constructor to modify $a
class B extends A {
public function __construct(){
// do something with $this->a value here;
}
}
or you'll always overwrite $a if your redeclare it as a property in B.
I just read your comment so I understand your use case a little better now. If you are adding/merging configurations in inheriting classes I'd suggest an alternative approach, adding some behaviour.
As you confirmed above:
class A has a default configuration
class B can optionally pass in config values that can update/add to the default config
In this case, something like this could work for you:
class A
{
protected $config = array(
'foo' => 'foo',
'bar' => 'bar',
'baz' => 'baz',
);
public function __construct(array $config = array())
{
$this->config = array_merge($this->config, $config);
}
public function getConfig()
{
return $this->config;
}
}
class B extends A
{
// implement
}
$b = new B(array(
'foo' => 'OVERWRITTEN',
'new' => 'NEW',
));
print_r($b->getConfig());
Yields:
Array
(
[foo] => OVERWRITTEN
[bar] => bar
[baz] => baz
[new] => NEW
)
You can also overwrite your default config in the same way when using class A directly.
Alternatively, instead of implementing the merge in __construct() you could implement that as a setConfig() method.
Hope this helps :)
EDIT
I just want to add one more thing: if your config is a multidimensional array, you will have to change how you merge arrays. At first glance array_merge_recursive() might seem like the obvious candidate. However:
$old = array(
'foo' => 'foo',
'bar' => 'bar',
'baz' => array(
'baa' => 'baa',
'boo' => 'boo',
),
);
$new = array(
'foo' => 'FOO',
'baz' => array(
'baa' => 'BAA',
),
'new' => 'new'
);
$merge = array_merge_recursive($old, $new);
print_r($merge);
actually yields:
Array
(
[foo] => Array
(
[0] => foo
[1] => FOO
)
[bar] => bar
[baz] => Array
(
[baa] => Array
(
[0] => baa
[1] => BAA
)
[boo] => boo
)
[new] => new
)
Probably not what you are looking for! Instead use array_replace_recursive():
$merge = array_replace_recursive($old, $new);
print_r($merge);
This yields:
Array
(
[foo] => FOO
[bar] => bar
[baz] => Array
(
[baa] => BAA
[boo] => boo
)
[new] => new
)
#Darragh I made it little different because I didn't want to change my constructors:
abstract class A
{
protected $a = array('a' => 1, 'b' => 2);
public function __construct()
{
$this->mixA();
}
protected function a()
{
return array();
}
protected function mixA()
{
foreach ($this->a() as $key => $val) {
$this->a[$key] = $val; // $val can be an array too (in my case it is)
}
}
}
class B extends A
{
protected function a()
{
return array(
'b' => 'new value',
'c' => 'new variable'
);
}
public function dumpA()
{
var_dump($this->a);
}
}
$b = new B();
$b->dumpA();
So now if I want to change my default configs I just overwrite a() method. mixA() method can be expanded as needed.
Related
I am using instanceof to check class instances and want to modify input data if class already has instance with same data. below example demonstrates my problem in detail where i have two unique array inputs and third one is duplicate array of second where instanceof supposed to work and modify input.
/**
* Foo Class
*/
class Foo {
public $bar = array();
public function __construct() {}
public function add( $bar ) {
if ( $bar['ID'] instanceof Baz ) { // inctanceof not working as i am expecting. supposed to modify duplicate occurrence
//if bar['ID'] is already instance of Baz then we are trying to modify bar ID before pass it so Baz.
$bar['ID'] = $bar['ID'] . rand();
$this->bar[ $bar['ID'] ] = new Baz( $bar );
}
else {
$this->bar[ $bar['ID'] ] = new Baz( $bar );
}
}
}
Class Baz
/**
* Class Baz
*/
class Baz {
public $ID;
public function __construct( $bar ) {
$this->ID = $bar['ID'];
}
}
Instance
$foo = new Foo();
$bar = array( 'ID' => 'bar1' );
$foo->add( $bar );
$bar2 = array( 'ID' => 'bar2' );
$foo->add( $bar2 );
$bar3 = array( 'ID' => 'bar2' ); //duplicate ID
$foo->add( $bar3 );
Print
print_r( $foo );
Output
Foo Object
(
[bar] => Array
(
[bar1] => Baz Object
(
[ID] => bar1
)
[bar2] => Baz Object
(
[ID] => bar2
)
)
)
Expected Output
Foo Object
(
[bar] => Array
(
[bar1] => Baz Object
(
[ID] => bar1
)
[bar2] => Baz Object
(
[ID] => bar2
)
[bar2{random number}] => Baz Object
(
[ID] => bar2{random number}
)
)
)
what i am doing wrong in here? kindly guide me and alternate solution is also applicable.
You Foo class should look like:
class Foo {
public $bar = array();
public function __construct() {}
public function add( $bar ) {
if (isset($this->bar[ $bar['ID'] ]) && $this->bar[ $bar['ID'] ] instanceof Baz ) { // inctanceof not working as i am expecting. supposed to modify duplicate occurrence
//if bar['ID'] is already instance of Baz then we are trying to modify bar ID before pass it so Baz.
$bar['ID'] = $bar['ID'] . rand();
$this->bar[$bar['ID']]= new Baz( $bar );
}
else {
$this->bar[ $bar['ID'] ] = new Baz( $bar );
}
}
}
Currently I'm working on a simple OOP script in PHP, it needs to compare the ID and the DATE of the array and sort them in the right order.
I was wondering why my constructor in the first class doesn't pass the $elements array properly.
The error I'm getting:
Notice: Undefined variable: elements in /Applications/XAMPP/xamppfiles/htdocs/strategy-pattern-.php on line 58
Catchable fatal error: Argument 1 passed to ObjectCollection::__construct() must be of the type array, null given, called in ... on line 58 and defined in ... on line 12
Code:
<?php
class ObjectCollection
{
var $elements = array(
array('id' => 2, 'date' => '2017-01-01',),
array('id' => 1, 'date' => '2017-02-01'));
var $comparator;
function __construct(array $elements)
{
$this->elements = $elements;
}
function sort()
{
if (!$this->comparator) {
throw new \LogicException('Comparator is not set');
}
uasort($this->elements, [$this->comparator, 'compare']);
return $this->elements;
}
function setComparator(ComparatorInterface $comparator)
{
$this->comparator = $comparator;
}
}
interface ComparatorInterface
{
function compare($a, $b);
}
class DateComparator implements ComparatorInterface
{
function compare($a, $b)
{
$aDate = new \DateTime($a['date']);
$bDate = new \DateTime($b['date']);
return $aDate <> $bDate;
}
}
class IdComparator implements ComparatorInterface
{
function compare($a, $b)
{
return $a['id'] <> $b['id'];
}
}
$collection = new ObjectCollection($elements);
$collection->setComparator(new IdComparator());
$collection->sort();
echo "Sorted by ID:\n <br>";
print_r($collection->elements);
$collection->setComparator(new DateComparator());
$collection->sort();
echo "<br>Sorted by date:\n <br>";
print_r($collection->elements);
?>
I know there may just be a rookie mistake somewhere but I'm really curious what I'm doing wrong haha.
Thanks in advance! :)
At the bottom of your script you have:
$collection = new ObjectCollection($elements);
However, the $elements variable is not defined. This is why you are getting the error.
The specific error is related to the fact that you used a type declaration in your class constructor requiring that an 'array' be passed. Prior to the addition of type declarations to php, the php runtime engine did not care what variables you passed, so long as you passed a number of variables equal to the number of required parameters to a function or method.
As also pointed out in another answer, many of us are assuming that your placement of the --
var $elements = array(
array('id' => 2, 'date' => '2017-01-01',),
array('id' => 1, 'date' => '2017-02-01'));
was never meant to be inside the class. With that said, doing so creates and initializes the $elements class variable, which is a valid technique that has many uses in OOP. However, the syntax used is obsolete, and if you really did want to initialize a class variable to a set value at object creation time, you should be using the syntax that includes a variable visibility keyword like:
protected $elements = array(
array('id' => 2, 'date' => '2017-01-01',),
array('id' => 1, 'date' => '2017-02-01'));
In conclusion, the answer to your question is that either you should define $collection to be an array at the bottom of the script, or pass an array in when you create the ObjectCollection object.
$collection = new ObjectCollection(array(
array('id' => 2, 'date' => '2017-01-01'),
array('id' => 1, 'date' => '2017-02-01'));
class ObjectCollection
{
// define as property
private $elements;
private $comparator;
function __construct(array $elements)
{
$this->elements = $elements;
}
function sort()
{
if (!$this->comparator) {
throw new \LogicException('Comparator is not set');
}
uasort($this->elements, [$this->comparator, 'compare']);
return $this->elements;
}
function setComparator(ComparatorInterface $comparator)
{
$this->comparator = $comparator;
}
}
...
// you need to define $elements to pass
$elements = array(
array('id' => 2, 'date' => '2017-01-01',),
array('id' => 1, 'date' => '2017-02-01'));
// them to the constructor
$collection = new ObjectCollection($elements);
// the way you did it, your $elements definition was in class scope so you got the error they are "NULL" / Not defined
You have declared the elements variable inside the class instead outside
$elements = array(
array('id' => 2, 'date' => '2017-01-01',),
array('id' => 1, 'date' => '2017-02-01'));
class ObjectCollection
{
I have a system where I am creating multiple classes that all extend from an abstract class.
Each class also declares 'settings' for that particular class type.
Example:
class First extends Base {
protected $name = 'First';
protected $lug = 'first';
protected $fields = [
'name',
'address',
'phone',
];
function __construct()
{
parent::__construct();
}
public function abstractMethod()
{
// do stuff for this particular class
}
}
and
class Second extends Base {
protected $name = 'Second';
protected $lug = 'second-one';
protected $fields = [
'first-name',
'last-name',
'email',
];
function __construct()
{
parent::__construct();
}
public function abstractMethod()
{
// do stuff for this particular class
}
}
Now what I want to be able to do is grab all extended classes and their 'settings' and return something like this:
$classes = [
'first' => [
'name' => 'First',
'slug' => 'first',
'fields' => ['name', 'address', 'phone']
],
'second' => [
'name' => 'Second',
'slug' => 'second-one',
'fields' => ['first-name', 'last-name', 'email']
]
];
So how would I go about doing this? Is there a better way?
I am using Laravel if that helps.
Edit: To Explain why not a duplicate
I'm not just after a way to get classes and their information I am after a way to architect this situation. I am essentially creating an extensible plugin system and need a way to Tell-Don't-Ask which plugins have been added.
I didn't try it, but it should work. Or it'll directs you.
$result = array();
foreach (get_declared_classes() as $class) {
if (is_subclass_of($class, 'Base'))
$result[] = get_class_vars($class);
}
But your properties needs to be public also.
What about using ReflectionClass? Getting properties is quite easy, example from manual below. Listing extended classes should be easy too.
<?php
class Bar {
protected $inheritedProperty = 'inheritedDefault';
}
class Foo extends Bar {
public $property = 'propertyDefault';
private $privateProperty = 'privatePropertyDefault';
public static $staticProperty = 'staticProperty';
public $defaultlessProperty;
}
$reflectionClass = new ReflectionClass('Foo');
var_dump($reflectionClass->getDefaultProperties());
Output:
array(5) {
["staticProperty"]=>
string(14) "staticProperty"
["property"]=>
string(15) "propertyDefault"
["privateProperty"]=>
string(22) "privatePropertyDefault"
["defaultlessProperty"]=>
NULL
["inheritedProperty"]=>
string(16) "inheritedDefault"
}
Using ReflectionObject you can do it like this:
$result = array();
foreach (get_declared_classes() as $class) {
if (is_subclass_of($class, 'Base')) {
$obj = new $class;
$refObj = new ReflectionObject($obj);
$props = $refObj->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);
$classProps = array();
foreach ($props as $prop) {
$property = $refObj->getProperty($prop->getName());
$property->setAccessible(true);
$classProps[$prop->getName()] = $property->getValue($obj);
}
$result[$class] = $classProps;
}
}
print_r($result);
Output:
Array (
[First] => Array (
[name] => First
[lug] => first
[fields] => Array (
[0] => name
[1] => address
[2] => phone
)
)
[Second] => Array (
[name] => Second
[lug] => second-one
[fields] => Array (
[0] => first-name
[1] => last-name
[2] => email
)
)
)
For a metering project, I have a class MeterReadings, with a constructor that takes a previous reading as basis for all of its property values except the timestamp. In the example, the Object has only 3 properties, in real life there are many more. I could line by line for each property X do
$this->propertyX=$prevMS->propertyX
but that quickly becomes tedious.
What is best practice for copying all property values from the $prevMS object?
<?php class MeterReadings{
function __construct($prevMS=NULL){
if($prevMS===NULL){
$this->gas=0;
$this->water=0;
$this->electricity=0;
}else{
//PHP can duplicate an object by assignment,
//so I expected to be able to create a copy of $prevMS
//but cannot $this=$prevMS;
//and cannot $this= clone $that;
//which both throw error 'Cannot re-assign $this'
foreach($prevMS as $key => $value){
//logs "Undefined property: MeterReadings::$0"
print "$key => $value\n";
$this->$key = $prevMS->$value;
}
}
$this->date=time();//set timestamp for this object
}
}
$test= new MeterReadings();
$test2 = new MeterReadings($test);
print_r($test);
print_r($test2);
?>gets me
gas => 0
water => 0
electricity => 0
date => 1434448706
MeterReadings Object
(
[gas] => 0
[water] => 0
[electricity] => 0
[date] => 1434448706
)
MeterReadings Object
(
[gas] =>
[water] =>
[electricity] =>
[date] => 1434448706
)
You can use clone and the __clone Magic Method to manage what get's cloned.
For example:
class Test
{
public $value1;
public $value2;
public $timestamp;
public function __clone()
{
$this->timestamp = null;
}
}
$test = new Test();
$test->value1 = 1;
$test->value2 = 2;
$test->timestamp = time();
$test2 = clone $test;
print_r($test2);
// Test Object
// (
// [value1] => 1
// [value2] => 2
// [timestamp] =>
// )
Alternative:
To keep things as in the __constuct as specified the comment below:
class Test
{
public $value1;
public $value2;
public $timestamp;
public function __construct (Test $copy = null)
{
if($copy) {
foreach ($copy as $key => $value) {
$this->$key = $value;
}
}
$this->timestamp = 'whoop';
}
}
$test = new Test();
$test->value1 = 1;
$test->value2 = 2;
$test->timestamp = 'whatev\'s';
$test2 = new Test($test);
print_r($test2);
// Test Object
// (
// [value1] => 1
// [value2] => 2
// [timestamp] => 'whoop'
// )
This will work for objects that are not itterable and for properties that are protected (but not private). For objects that are itterable and return something else, you can use get_object_vars(), documentation here. For stuff that's private (are you sure you need private, that's unusual) you'll need to create getters. They can be protected if you don't want them externally available.
I have a following class hierarchy, which shown in a reproduction script below:
<?php
header('Content-Type: text/plain');
class A
{
public $config = array(
'param1' => 1,
'param2' => 2
);
public function __construct(array $config = null){
$this->config = (object)(empty($config) ? $this->config : array_merge($this->config, $config));
}
}
class B extends A
{
public $config = array(
'param3' => 1
);
public function __construct(array $config = null){
parent::__construct($config);
// other actions
}
}
$test = new B();
var_dump($test);
?>
Output:
object(B)#1 (1) {
["config"]=>
object(stdClass)#2 (1) {
["param3"]=>
int(1)
}
}
What I wanted, is that A::$config not be overriden by B::$config. There might be a lot of descendant classes from B, where I would like to change $config, but I need that those $config values to merge / overwrite if match $config values of all it's parents.
Q: How can I do that ?
I've tried to use array_merge() but in non-static mode those variables just override themselves. Is there a way to achieve merge effect for class tree without static (late static binding) ?
Instead of declaring a $config property with values that you're going to change in the constructor, it's better to declare those values as default values. This is also described in Orangepill's answer:
class A
{
public $config;
private $defaults = array(
'param1' => 1,
'param2' => 2,
);
public function __construct(array $config = array())
{
$this->config = (object)($config + $this->defaults);
}
}
A few twists there; by declaring the default value of the $config constructor argument as an empty array, you can simplify your code by using array operators like I did above. Undefined keys in $config are filled in by $this->defaults.
The extended class will look very similar:
class B extends A
{
private $defaults = array(
'param3' => 1
);
public function __construct(array $config = array())
{
parent::__construct($config + $this->defaults);
}
}
You can restructure how your extended class is instantiated
class B extends A
{
private $defaults = array('param3' => 1);
public function __construct(array $config = null){
parent::__construct($config?array_merge($this->defaults, $config):$this->defaults);
}
}
You can do that using ReflectionClass. Start by introspecting $this, use getProperty(), then use getParentClass() to do the same on the parent class and its parent etc. and merge the resulting arrays together.
This is probably not the best solution for the problem you're facing though.
I believe the following is what you are looking for. Adapted from Inherit static properties in subclass without redeclaration?
<?php
class MyParent {
public static $config = array('a' => 1, 'b' => 2);
public static function getConfig() {
$ret = array();
$c = get_called_class();
do {
$ret = array_merge($c::$config, $ret);
} while(($c = get_parent_class($c)) !== false);
return $ret;
}
}
class MyChild extends MyParent {
public static $config = array('a' => 5, 'c' => 3, 'd' => 4);
public function myMethod($config) {
$config = array_merge(self::getConfig(), $config);
}
}
class SubChild extends MyChild {
public static $config = array('e' => 7);
}
var_export(MyChild::getConfig());
// result: array ( 'a' => 5, 'b' => 2, 'c' => 3, 'd' => 4, )
$mc = new MyChild();
var_export($mc->myMethod(array('b' => 6)));
// result: array ( 'a' => 5, 'b' => 6, 'c' => 3, 'd' => 4, )
var_export(SubChild::getConfig());
// result: array ( 'a' => 5, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 7, )