Carson
Carson

Reputation: 451

PHP static member inheritance

I have this base class:

<?php

use Parse\ParseObject;
use Parse\ParseQuery;

class BaseModel extends ParseObject
{

    public static $className = 'PlaceHolder';

    public static function query() {
        return new ParseQuery(self::$className);
    }

}

And this child class

<?php

class Post extends BaseModel
{

    public static $className = 'Post';

}

When I call Post::$className I get 'Post' but when I use Post::query() it uses the parent classes value 'PlaceHolder'.

Why does the inherited static function use the value from the parent class?

Upvotes: 2

Views: 138

Answers (1)

TiiJ7
TiiJ7

Reputation: 3392

The query function is defined in the parent class and will thus use the value of that class. This is a limitation of the self keyword. You will want to look into Late Static Binding to get around this.

public static function query() {
    return new ParseQuery(static::$className);
}

Upvotes: 3

Related Questions