0

Here is the code:

class Crud {
 public static function get($id);
 echo "select * from ".self::$table." where id=$id";// here is the problem
}

class Player extends Crud {
 public static $table="user"
}


Player::get(1);

I could use Player::$table, but Crud will be inherited in many classes.

Any idea ?

2 Answers 2

2

To refer to static members in PHP there are two keywords :

  • self for "static" binding (the class where it is used)

  • static for "dynamic"/late static binding (the "leaf" class)

In your case you want to use static::$table

Sign up to request clarification or add additional context in comments.

3 Comments

You can add parent for the parent Class when extends is used.
There's indeed parent to refer to the parent's class
I miss for overrides
1

You want to use static:

<?php
class Crud {
    public static $table="crud";
    public static function test() {
       print "Self: ".self::$table."\n";
       print "Static: ".static::$table."\n";
    }
}

class Player extends Crud {
    public static $table="user";
}

Player::test();

$ php x.php 
Self: crud
Static: user

Some explanation from the documentation: http://php.net/manual/en/language.oop5.late-static-bindings.php

"Late binding" comes from the fact that static:: will not be resolved using the class where the method is defined but it will rather be computed using runtime information. It was also called a "static binding" as it can be used for (but is not limited to) static method calls.

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.