0

I have problem about calling a static property of a class inside another class.

Class A {

    public $property;

    public function __construct( $prop ) {
        $this->property = $prop;

    }
    public function returnValue(){
        return static::$this->property;
    }

}

Class B extends A {

    public static $property_one = 'This is first property';
    public static $property_two = 'This is second property';

}

$B = new B( 'property_one' );
$B->returnValue();

I expect to return This is first property But the Output is just the name a parameter input in __construct;

When I print_r( static::$this->property ); the output is just property_one

Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
Pars
  • 4,932
  • 10
  • 50
  • 88
  • You are instantiating `A` which never sets or has access to `$property_one`. So the expected result will never be returned – tlenss Oct 22 '13 at 09:46
  • Sorry I edited post, actually that was instantiating B. sorry for typo. – Pars Oct 22 '13 at 09:47
  • 2
    You can't return a value in the constructor, `new B` always gives you an object – pNre Oct 22 '13 at 09:48
  • I edited post, the goal is I want to get the value of static $property_one from returnValue method. – Pars Oct 22 '13 at 09:52

3 Answers3

1

Maybe like this?

<?php
Class A {

    public $property;

    public function __construct( $prop ) {
        $this->property = $prop;
        print static::${$this->property};
    }
}

Class B extends A {

    public static $property_one = 'This is first property';
    public static $property_two = 'This is second property';

}

$B = new B( 'property_one' );

(I mean you can access (print,...) the property this way, but the constructor will return an object anyway.)

Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
  • This code won't work... it just won't (access a child's static property in the parent's constructor. [Just test this snippet!](http://codepad.org/x3wZ6Trp) – Elias Van Ootegem Oct 22 '13 at 10:01
  • codepad uses php 5.2 or something similar. Late static binding started to be supported I think from php 5.3. Details: http://php.net/manual/en/language.oop5.late-static-bindings.php – Lajos Veres Oct 22 '13 at 10:21
1

Just change:

return static::$this->property;

with:

return static::${$this->property};
pNre
  • 5,376
  • 2
  • 22
  • 27
1

There are several issues here:

  1. the static property $property_one is declared in class B, the A class's constructor won't have access to that property, nor can you guarantee this property to be present.
    Granted, since PHP 5.3, late static binding is supported, but that doesn't change the fact that you're never going to be sure that some static property that just happens to be called whatever $this->property happens to be assigned. What if it's assigned an object? an int, or float?
  2. You access a static property like this: static::$propery or self::$property. Note the $! When you write static::$this->property, you're expecting this to evaluate to self::property_one. You're clearly missing the $ sign.
    The very least you need is self::${$this->property}. Check the PHP manual on variable variables.
  3. You're attempting to return a string from a constructor function, that's not possible. A constructor must, must return an instance of the class. Any return statements that don't will be ignored.

To have access to a static property of a child class in the constructor, you can't but rely on the child's constructor:

Class A
{
    public $property;
}

Class B extends A
{
    public static $property_one = 'This is first property';
    public static $property_two = 'This is second property';
    public function __construct( $prop )
    {
        $this->property = $prop;
        print self::${$this->property};
    }
}
$B = new B( 'property_one' );

An alternative would be:

Class A
{
    public $property;
    public function __constructor($prop)
    {
        $this->property = $prop;
    }
    public function getProp()
    {
        return static::${$this->property};
    }
}

Class B extends A
{
    public static $property_one = 'This is first property';
    public static $property_two = 'This is second property';
}
$B = new B( 'property_one' );
$B->getProp();
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • actually, I edited post now, but the base code is from Laravel 4. And by static::${$this->property} it is working well. – Pars Oct 22 '13 at 09:56
  • @aliA: I was editing my answer, adding code examples and more details... added a second example that fits your now edited question perfectly. – Elias Van Ootegem Oct 22 '13 at 10:00