Pars
Pars

Reputation: 5262

Call static properties within another class in php

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

Upvotes: 0

Views: 888

Answers (3)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76405

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();

Upvotes: 1

pNre
pNre

Reputation: 5376

Just change:

return static::$this->property;

with:

return static::${$this->property};

Upvotes: 1

Lajos Veres
Lajos Veres

Reputation: 13725

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.)

Upvotes: 1

Related Questions