0

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.

2
  • 1
    OOPs is not clear in you mind. Object has it's own property and bounded with only that object. Commented Jan 20, 2011 at 8:05
  • declare class property as static Commented Jan 20, 2011 at 8:06

3 Answers 3

1

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";
?>
Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

1

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.

2 Comments

I dont want to use it by static. Also i cannot use it like that because there are a lot of classes that will use the parent $a, some of them in functions and i cannot use global everywhere.
@keepwalking Sorry, but I don't understand. There is neither a static, nor a global anywhere in the examples above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.