Page 1 of 1

Just Experts -- Need Help

Posted: Mon Aug 03, 2009 4:10 am
by dzeck
Hi all,
I have a problem with a superclass.

Code: Select all

 
class Model {
    
    public function render(){
        echo '<br />class: '.get_class($this).' -- function: '.__FUNCTION__;
    }
    
}
 
class Product extends Model {
    
    public function show(){
        $this->render();
    }
}
 
class User extends Model {
    
    public function index(){
        $this->render();
    }
}
 
 
$p = new Product();
$u = new User();
 
echo $p->show();
echo $u->index();
 
results:
class: Product -- function: render
class: User -- function: render

How to get child called function on parent class function. For example render instead of index etc.
I'll very happy every advise and every idea ?

Thanks.

Sorry for poor english.

Re: Just Experts -- Need Help

Posted: Mon Aug 03, 2009 8:07 am
by jackpf
Hey,

Sorry, just to be clear, you want to display:

class: Product -- function: show
class: User -- function: index

Instead?

If so, you'll have to send __FUNCTION__ as an argument to Model::render().

It is odd the way it shows the right class though...

Re: Just Experts -- Need Help

Posted: Mon Aug 03, 2009 6:43 pm
by m4rw3r
It is not odd at all that it shows a "different class".
$this is an instance of User in the case of $u->index(), and $this doesn't magically change type once you call a method inherited from parent class.

If __CLASS__ would have shown User on that call, then it would have been strange.

Re: Just Experts -- Need Help

Posted: Tue Aug 04, 2009 6:44 am
by jackpf
Oh right...I thought $this was just a reference to the current class.

Thanks for the clarification.

Re: Just Experts -- Need Help

Posted: Wed Aug 05, 2009 5:35 pm
by Ollie Saunders
To solve the function name part of your question, try debug_backtrace().

Re: Just Experts -- Need Help

Posted: Thu Aug 06, 2009 4:56 pm
by josh
jackpf wrote:Oh right...I thought $this was just a reference to the current class.

Thanks for the clarification.

Code: Select all

 
class A
{
function foo () { B::foo();  //  A }
}
class B
{
static function foo() { echo get_class( $this ); }
}
B:foo(); //  error