PHP Class Parent Instance (Singleton) - php

The scenario is this
class a
{
public $val;
}
class b extends a
{
}
class c extends b
{
}
$one = new b();
$one->val = "a value";
$other = new c();
echo $other->val;
// wanted 'a value', got ''
So the result i need here is: "a value", but of course is blank.
What i need is that the 'a' class to always be used as an instance in 'b'. So whenever i use a class that extends the 'b', the parent 'a' class to be inhereted as an instance.

If you read the php manual on the static keyword it gives an example of exactly what you are trying to do. You can read about it here: http://www.php.net/manual/en/language.oop5.static.php
Here is the example code they use.
<?php
class Foo
{
public static $my_static = 'foo';
public function staticValue() {
return self::$my_static;
}
}
class Bar extends Foo
{
public function fooStatic() {
return parent::$my_static;
}
}
print Foo::$my_static . "\n";
$foo = new Foo();
print $foo->staticValue() . "\n";
print $foo->my_static . "\n"; // Undefined "Property" my_static
print $foo::$my_static . "\n";
$classname = 'Foo';
print $classname::$my_static . "\n"; // As of PHP 5.3.0
print Bar::$my_static . "\n";
$bar = new Bar();
print $bar->fooStatic() . "\n";
?>

Since $other = new c(); is actually creating a new instance, it is not possible.
but if you declare val as Static member, you will have the result that you want.
<?
class a
{
public static $val;
}
class b extends a
{
}
class c extends b
{
}
$one = new b();
a::$val = "a value";
echo c::$val;

Here is how to do it without Inheritance:
class A
{
public $foo;
}
class B {
public function __construct(A $a)
{
$this->a = $a;
}
}
class C {
public function __construct(A $a)
{
$this->a = $a;
}
}
$a = new A;
$b = new B($a);
$c = new C($a);
$b->a->val = 'one value';
echo $c->a->val;
If you dont like having to fetch $a first to get to val, you could assign by reference
class A
{
public $foo;
}
class B {
public function __construct(A $a)
{
$this->val = &$a->val;
}
}
class C {
public function __construct(A $a)
{
$this->val = &$a->val;
}
}
$a = new A;
$b = new B($a);
$c = new C($a);
$b->val = 'one value';
echo $c->val;
Though personally I find the first approach more maintainable and clear.

Related

Does this smell if I cant decide which descendant should I use?

abstract class X
{
private $v;
protected function setV($v)
{
$this->v = $v;
}
public function getV()
{
return $v;
}
}
class A extends X
{
public function doIt()
{
parent::setV(1);
}
}
class B extends X
{
public function doIt()
{
parent::setV(2);
}
}
$a = new A();
$a->doIt();
$b = new A();
$b->doIt();
but if I want to use getV(), I can both call
$a->getV() and $b->getV()
which sounds silly. Which one to use? To be honest, I would like to see something like that:
X::getV();
which is not possible, an instance must be exists/
It depends on "what do you want". Firstable, it's possible to use X::getV() method, but you need to make v member and getV method static, as shown below.
<?php
abstract class X
{
private static $v;
protected static function setV($v)
{
self::$v = $v;
}
public static function getV()
{
return self::$v;
}
}
class A extends X
{
public function doIt()
{
self::setV(2);
}
}
class B extends X
{
public function doIt()
{
self::setV(1);
}
}
$a = new A();
$a->doIt();
echo X::getV();
// prints 2
// but be aware, that ANY instance of X children class will change the same X::$v value
$b = new B();
$b->doIt();
echo X::getV();
// prints 1
Static members (like X::$v) are stored only once, they are "binded" to the class, not to the instance of this class.
<?php
class Foo
{
public static $v = 5;
}
$instance1 = new Foo();
$instance2 = new Foo();
echo Foo::$v;
echo $instance1::$v;
echo $instance2::$v;
// prints 5, 5, 5
$instance1::$v = 10;
echo Foo::$v;
echo $instance1::$v;
echo $instance2::$v;
// prints 10, 10, 10

Cannot create object in class PHP

I try to create object in PHP class, but i get some interesting errors in IDE, like unexpected ( token etc. Here is my code:
class A {
public $a = 1;
}
class B {
$aa = new A();
}
Where is the problem?
In PHP, you can only assign "fixed" values to properties in the class definition.
class A {
public $a = 3; // will work
public $b = "hello"; // will work
public $c = foo(); // won't work
public $d = new Foo(); // won't work
}
If you want to do so, you can use the __construct() method which will be called every time a new instance is created or any other method that you call.
class B {
public $aa; // define visibility of $aa
function __construct() {
$this->aa = new A();
}
}
You need to make a constructor on class A
class A {
function __construct() {
$this->a = 1;
}
public function returnA() {
return $this->a;
}
}
$aa = new A();
echo $aa->returnA();
Try to create a constructor in class A and see if it works:
class A {
public $a;
function __construct()
{
$this->$a = 1;
}
}
class B {
$aa = new A();
}

Simulate multiple inheritance in PHP

Assuming I have 2 classes
Class A {
public function doA(){
echo "Im A";
}
}
Class B {
public function doB(){
echo "Im B";
}
}
write Class C, in order that the following code runs:
$c = new C();
$c->doA();
$c->doB();
and outputs:
>> Im A
>> Im B
This was in a test, and the conditions where:
use no static calls
you can't modify class A or class B
so I wrote:
Class C {
public function doA() {
$a = new A();
$a->doA();
}
public function doB() {
$b = new B();
$b->doB();
}
}
So apparently I was wrong as it can be "more optimized"
can someone tell me how to do it?
You could keep instances of A and B instead of instantiating them each time.
class C {
private $a, $b;
public __construct() {
$this->a = new A();
$this->b = new B();
}
public function doA() {
$this->a->doA();
}
public function doB() {
$this->b->doB();
}
}
PHP has no "native" multiple inheritance, but you can achieve something similar to it by using traits.
Trait A {
public function doA(){
echo "Im A";
}
}
Trait B {
public function doB(){
echo "Im B";
}
}
Class C {
use A, B;
}
$c = new C;
$c->doA();
$c->doB();
Note that this would require at least PHP 5.4.
To do it without modifying classes, the best and optimised option would be as follows.
class C {
private $a;
private $b;
public __construct() {
$this->a = new A();
$this->b = new B();
}
public function __call($method, $arguments = array()) {
if(method_exists($this->as, $method)) {
return call_user_func(array($this->a, $method));
}
}
}
The above is also future proof, so adding new methods would also follow.
While you were told not to modify classes A and B, the correct way to do this would be by having B extend A, then having C extend B, like below.
class A {
public function doA(){
echo "Im A";
}
}
class B extends A {
public function doB(){
echo "Im B";
}
}
class C extends B {
}
$c = new C();
$c->doA();
$c->doB();

get a list of all variables defined outside a class by user

i have something like this:
class foo
{
//code
}
$var = new foo();
$var->newVariable = 1; // create foo->newVariable
$var->otherVariable = "hello, im a variable"; //create foo->otherVariable
i can get in class foo a list of all variables defined outside by user (newVariable, otherVariable,etc)? Like this:
class foo
{
public function getUserDefined()
{
// code
}
}
$var = new foo();
$var->newVariable = 1; // create foo->newVariable
$var->otherVariable = "hello, im a variable"; //create foo->otherVariable
var_dump($var->getUserDefined()); // returns array ("newVariable","otherVariable");
Thanks!.
Yes, using get_object_vars() and get_class_vars():
class A {
var $hello = 'world';
}
$a = new A();
$a->another = 'variable';
echo var_dump(get_object_vars($a));
echo '<hr />';
// Then, you can strip off default properties using get_class_vars('A');
$b = get_object_vars($a);
$c = get_class_vars('A');
foreach ($b as $key => $value) {
if (!array_key_exists($key,$c)) echo $key . ' => ' . $value . '<br />';
}
What is your goal? Imo it's not very good practice (unless you really know what you are doing). Maybe it's good idea consider create some class property like "$parameters" and then create setter and getter for this and use it in this way:
class foo {
private $variables;
public function addVariable($key, $value) {
$this->variables[$key] = $value;
}
public function getVariable($key) {
return $this->variables[$key];
}
public function hasVariable($key) {
return isset($this->variables[$key]);
}
(...)
}
$var = new foo();
$var->addVariable('newVariable', 1);
$var->addVariable('otherVariable', "hello, im a variable");
And then you can use it whatever you want, for example get defined variable:
$var->getVariable('otherVariable');
To check if some var is already defined:
$var->hasVariable('someVariable')
get_class_vars() http://php.net/manual/en/function.get-class-vars.php
You question is not clear though.
$var->newVariable = 1;
there are two possible contex of above expression
1) you are accessing class public variables.
like
class foo
{
public $foo;
public function method()
{
//code
}
}
$obj_foo = new foo();
$obj_foo->foo = 'class variable';
OR
2) you are defining class variable runtime using _get and _set
class foo
{
public $foo;
public $array = array();
public function method()
{
//code
}
public function __get()
{
//some code
}
public function __set()
{
// some code
}
}
$obj_foo = new foo();
$obj_foo->bar= 'define class variable outside the class';
so in which context your question is talking about?

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;
}

Categories