mkaminski Posted September 11, 2006 Share Posted September 11, 2006 Hello,Say I have a class such as this..class A{ public function sayHello() { print("hello"); }}class B extends A{ public function sayHello() { print("A more refined hello...perhaps bonjour?"); }}$object = new b();Now $object is of type B. Calling $object->sayHello() would output "A more refined hello...perhaps bonjour?"Now how can I call the sayHello() so that it will call the A class' sayHello() method, simply outputting "hello"? I know how to do this in Java...it would just be a simple type cast... but how can this be done in PHP 5? I want to change the type of the object, not do a parent::sayHello() in class B's sayHello() method.Thanks! Quote Link to comment Share on other sites More sharing options...
perezf Posted September 11, 2006 Share Posted September 11, 2006 just typesayhello(); Quote Link to comment Share on other sites More sharing options...
mkaminski Posted September 11, 2006 Author Share Posted September 11, 2006 sorry I dont think that will do it Quote Link to comment Share on other sites More sharing options...
jefkin Posted September 11, 2006 Share Posted September 11, 2006 you can create an access method in class bclass B extends A{ /// your stuff function A_Hello() { parent::sayHello(); }}For a non invasive means, if A's sayHello method doesn't involve any class variables, you can sayA::sayHello();But the first is probaly a safer bet for anything non-trivial, and deffinitely for any method that uses class variables (instance variable). Sometimes Global class variables will be okay.So, the absolute safest way is the first one. Use that if at all possible.Jeff Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.