See if there is a static property in the child class from the parent class (late static binding)?

Code in parent class:

foreach(static::$_aReadOnlyDatabaseTables AS $TableName => $aColumns){
  // Do something
}

This works when $ _aReadOnlyDatabaseTables is defined in the child class, but throws an error when $ _aReadOnlyDatabaseTables is missing. I need to check if this property exists first.

I think this should happen something like this:

if(property_exists(static,$_aReadOnlyDatabaseTables)){
   foreach(static::$_aReadOnlyDatabaseTables AS $TableName => $aColumns){
      // Do something
   }
}

But this causes a syntax error unexpected ',', expecting T_PAAMAYIM_NEKUDOTAYIM. Using $thisinstead staticdoes not work either; it always evaluates to false.

What is the correct syntax for this?

+6
source share
3 answers

You should try the following:

if(property_exists(get_called_class(), '_aReadOnlyDatabaseTables')) {
   foreach(static::$_aReadOnlyDatabaseTables AS $TableName => $aColumns){
      // Do something
   }
}
+9
source

( ) . , , .

, , , , .

+3

You can do this quickly and dirty, using get_class()instead of the keyword static:

if (property_exists(get_class($this), '_aReadOnlyDatabaseTables')) { ... }
0
source

All Articles