How to access constant defined in child class from parent class functions?
Posted
by kavoir.com
on Stack Overflow
See other posts from Stack Overflow
or by kavoir.com
Published on 2010-03-12T03:35:44Z
Indexed on
2010/03/12
3:47 UTC
Read the original article
Hit count: 145
I saw this example from php.net:
<?php
class MyClass {
const MY_CONST = "yonder";
public function __construct() {
$c = get_class( $this );
echo $c::MY_CONST;
}
}
class ChildClass extends MyClass {
const MY_CONST = "bar";
}
$x = new ChildClass(); // prints 'bar'
$y = new MyClass(); // prints 'yonder'
?>
But $c::MY_CONST is only recognized in version 5.3.0 or later. The class I'm writing may be distributed a lot.
Basically, I have defined a constant in ChildClass and one of the functions in MyClass (father class) needs to use the constant. Any idea?
© Stack Overflow or respective owner