Re: [PHP] Assigning functions

[email protected] (Philip Thompson)
Newsgroups php.general
Message-ID <[email protected]>
On May 2, 2008, at 2:42 PM, Craige Leeder wrote:

> Hello Philip
>
> First thing first: design patterns are your friend. A good reference
> for which, is:
>
> http://www.fluffycat.com/PHP-Design-Patterns/
>
> Second of all. What is the situation in which you are trying to do
> this? I can't really think of one where you would do such a thing.
>
> - Craige

Sorry I haven't responded earlier... was away from my computer since  
Friday!

First, thanks for the link. Second, because of the way our application  
is setup, I am interested in doing this functionality. However,  
someone pointed out a very good point - basically abstraction and  
classes not needing to know about other classes. So, 'A' knowing about  
'C' is "not a good thing" (TM). After thinking twice about it, I won't  
take this approach.

However, the reason this came up is because the application is split  
up logically between the database layer, security layer, view layer,  
logic layer, etc... There is a core class which has generic functions  
in it which is called on every request. In this class, other classes  
are instantiated and used throughout the app. Kinda like this...

<?php
class Core {
     $this->site = new Site ();
}

class Site {
     $this->render = new Render ();
}

class Render {
     function translate ($template, $arr) {
         $content = str_replace (
             array_keys($arr),
             array_values($arr),
             "$template.tpl.php"
         );
         return $content;
     }

     function stroke () { }
}

// Somewhere in the app...
$content = $core->site->render->translate('someTemplate', $replace);
?>

Obviously, these are VERY dumbed-down versions of the classes, but  
this is the general idea. It all comes down to separation of business  
logic from the view from the database, and so on.

Hopefully that's as clear as mud now.

~Philip


> On Fri, May 2, 2008 at 3:09 PM, Philip Thompson <[email protected] 
> > wrote:
>> Hi all. I have several classes. Within each class, a new class is  
>> called. Is
>> there a way to assign a function in a *deeper* class to be called  
>> in the
>> first class? Example to follow......
>>
>> <?php
>> class A {
>>    function __construct () {
>>        $this->b = new B ();
>>        // I want to do the following. This does not work, of course.
>>        $this->doSomething = $this->b->c->doSomething;
>>    }
>> }
>>
>> class B {
>>    function __construct () {
>>        $this->c = new C ();
>>    }
>> }
>>
>> class C {
>>    function __construct () { }
>>    function doSomething () { echo "¡Hi!"; }
>> }
>>
>> $a = new A ();
>> // Instead of doing this,
>> $a->b->c->doSomething();
>>
>> // I want to do this.
>> $a->doSomething(); // ¡Hi!
>> ?>
>>
>> Basically, it's just to shorten the line to access a particular  
>> function.
>> But, is it possible?!
>>
>> Thanks,
>> ~Philip
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.