Just Experts -- Need Help

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
dzeck
Forum Newbie
Posts: 1
Joined: Mon Aug 03, 2009 4:00 am

Just Experts -- Need Help

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Just Experts -- Need Help

Post 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...
User avatar
m4rw3r
Forum Commoner
Posts: 33
Joined: Mon Aug 03, 2009 4:19 pm
Location: Sweden

Re: Just Experts -- Need Help

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Just Experts -- Need Help

Post by jackpf »

Oh right...I thought $this was just a reference to the current class.

Thanks for the clarification.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Just Experts -- Need Help

Post by Ollie Saunders »

To solve the function name part of your question, try debug_backtrace().
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Just Experts -- Need Help

Post 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
 
Post Reply