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 absent. I need to check if this property exists first.
I think it should go something like this:
if(property_exists(static,$_aReadOnlyDatabaseTables)){
foreach(static::$_aReadOnlyDatabaseTables AS $TableName => $aColumns){
// Do something
}
}
But this throws a syntax error, unexpected ',', expecting T_PAAMAYIM_NEKUDOTAYIM
. Using $this
in place of static
doesn't work either, it always evaluates false.
What is the proper syntax for this?
Late static binding was a feature introduced with php 5.3. It allows us to inherit static methods from a parent class, and to reference the child class being called.
Static properties are accessed using the Scope Resolution Operator ( :: ) and cannot be accessed through the object operator ( -> ). It's possible to reference the class using a variable.
In PHP, if a static attribute is defined in the parent class, it cannot be overridden in a child class.
The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class. The static keyword is also used to declare variables in a function which keep their value after the function has ended.
You should try this:
if(property_exists(get_called_class(), '_aReadOnlyDatabaseTables')) {
foreach(static::$_aReadOnlyDatabaseTables AS $TableName => $aColumns){
// Do something
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With