Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I'm trying to get the code below to work in child classes. But it keeps failing because it is checking the basicDbClass rather than the child class.

For those complaining and voting my question down because of my use of static methods and variables, the static getBy method serves the purpose of returning an array of matching class objects. If you have a complaint about that methodology, then rather voting down every question that uses code you don't agree with, look for the next question about that subject and then give them your brilliant answer. If you're really antsy, I'll even do you the favor of asking that question so that you can get this out of your system.

The main problem is in static::$data_table. When I do the following, I get a fatal error:

access to undeclared static property: basicDbClass::$data_table

$obj_array = childClass::getBy('id',5);

class basicDbClass {

    public static function getBy($property,$value) {
        if(!property_exists(get_called_class(),$property) ) {
            debug::add('errors',__FILE__,__LINE__,__METHOD__,$property . ' is not a valid property.');
            return false;
        $obj_array = array();
        $sql = sprintf('SELECT `id` FROM `%s` WHERE `%s` = \'%s\'',static::$data_table,mysql_real_escape_string($property),mysql_real_escape_string($value));
        $result = mysql_query($sql);
        if($result === false) {
            return false;
        while($row = mysql_fetch_assoc($result)) {
            $obj_array[$row['id']] = new static($row['id']);
        return $obj_array;

class childClass extends basicDbClass {
  protected static $data_table = 'child_class_table';
  protected $name;
  protected $id;
share|improve this question
Can you please confirm childClass is a subclass of basicDBClass and edit your question to reflect that? –  Abhi Beckert Aug 19 '12 at 4:57
@AbhiBeckert yes that is just a typo. I want to fix it but it won't let me edit my post because there is a pending suggested edit, and I don't have enough rep to confirm that edit. That seems kind of stupid. How can I not be allowed to accept an edit to my own question? –  Buttle Butkus Aug 19 '12 at 6:35
If you are using static members, it is not OOP. Please go and learn about dependency injection. And while you are at it, stop using the outdated mysql_* functions. –  teresko Aug 19 '12 at 7:31
@teresko: err... - what? Static members are just as much part of OOP as non-static members. They just don't get inherited like non-static members, in PHP at least. –  tdammers Aug 19 '12 at 9:40
@tdammers , then please explain, how static variables are different from global variables and how static methods are different from simple namespaced functions. –  teresko Aug 19 '12 at 9:46

2 Answers 2

up vote 4 down vote accepted

First of all I think you might have a typo, childClass doesn't extend basicDbClass.

With that said, as long as you are using PHP 5.3.0 or later, the static keyword should work as described in the documentation for "late static bindings" which provides the polymorphic behavior for static properties that you are looking for (this is weird to me, since I am more accustomed to the inheritance rules in Java).

If you do not define $data_table in the parent class basicDbClass, any call of basicDbClass::getBy on that class directly would fail with a similar error to what you described.

Here's a simple example that shows what should work:


class A {
    protected static $value = 'From class A';

    public static function output() {
        echo static::$value;

class B extends A{
    protected static $value = 'From class B';

A::output(); // "From class A"
B::output(); // "From class B"
share|improve this answer
Yes that is a typo but it won't let me edit my post while there is an edit pending. And it also won't let me accept the edit. Makes sense, right? –  Buttle Butkus Aug 19 '12 at 6:39

What version of PHP are you using? This code works without any errors for me using PHP 5.3.13:

// output: child_class_table

class basicDbClass {
  public static function getBy() {

class childClass extends basicDbClass {
  protected static $data_table = 'child_class_table';
share|improve this answer
I'm using PHP I used static in the static methods and $this in the object methods and now everything is working. Thanks so much! –  Buttle Butkus Aug 19 '12 at 6:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.