2

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?

2
  • The query function is in the parent class, and as such self will always be the parent class, it doesn't matter that you've redefined the variable in another class. Commented Apr 6, 2015 at 11:38
  • The simple solution would be to just pass the classname in as an argument instead. Commented Apr 6, 2015 at 11:39

1 Answer 1

3

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

Comments

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.