Re: [PHP] Assigning functions

2008-05-05 Thread Philip Thompson

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...


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..

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


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Assigning functions

2008-05-02 Thread David Otton
2008/5/2 Philip Thompson <[EMAIL PROTECTED]>:
> 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..
>
>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?!

Inheritance - http://uk.php.net/extends

class A {
  function methodOfA() {}
}

class B extends A {}

B::methodOfA();

(use sparingly - too much inheritance results in a tightly-coupled
hierarchy of classes, which are brittle and hard to re-use).

Or simply express the method in the interface of the exposed class:

class A {
  function methodOfA() {}
}

class B {
  function methodOfA() {
A::methodOfA();
  }
}

b::methodOfA();

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Assigning functions

2008-05-02 Thread Nathan Nobbe
On Fri, May 2, 2008 at 1:48 PM, Nathan Nobbe <[EMAIL PROTECTED]> wrote:

> On Fri, May 2, 2008 at 1: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..
>>
>> > 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?!
>
>
> i cant remember what the term is for it phillip (ill look later)
>

i remember what it is know; the principal of least knowledge ;)

http://en.wikipedia.org/wiki/Law_of_Demeter

-nathan


Re: [PHP] Assigning functions

2008-05-02 Thread Nathan Nobbe
On Fri, May 2, 2008 at 2:03 PM, Craige Leeder <[EMAIL PROTECTED]> wrote:

> ons here) as a simple example something like this
> >
> >  class B {
> >   //...
> >   function doSomething() {
> >   return $this->c->doSomething();
> >   }
> >  }
> >
> >  which allows you this in A instances
> >
> >  $this->b->doSomething();
> >
> >  this is the preferred approach, since A and C instances are loosely
> coupled.
> >
> >  of course, if you wanted a to 'know' about c then you could do something
> >  like this,
> >
> >  class B {
> >   // ..
> >   function getC() {
> >  return $this->c;
> >   }
> >  }
> >
> >  giving you the ability to do this in A instances
> >
> >  $this->b->getC()->doSomething();
> >
> >  of course now A's knows about C's and your system is more tightly
> coupled.
> >
> >  -nathan
> >
>
> Why don't you just do a registry pattern instance then? IE:
>
> class Registry
> {
>  private satic objs;
>
>  public function __construct()
>  {
>
>self::$objs = function_get_args();
>  }
>
>  public static function __get($obj)
>  {
>return self::$objs[$obj];
>  }
> }
>
> class A
> {
> ...
> }
>
> class B
> {
> ...
> }
>
> $reg = new Registry( new A(), new B());
>
> Now A and B can access each other through Registry::A and Registry::B
>
> (that code may not function. It's just a general example)


that seems like overkill to me, and anyway, A instances need to get at C
instances.. the Registry would need to be globally available and even then,
if C's and A's could talk to each other through it, it still presents the
issue where A instances know about C instances.  its a design decision to be
sure, but likely the cleanest solution will have A instances not knowing
about C instances.

-nathan


Re: [PHP] Assigning functions

2008-05-02 Thread Craige Leeder
ons here) as a simple example something like this
>
>  class B {
>   //...
>   function doSomething() {
>   return $this->c->doSomething();
>   }
>  }
>
>  which allows you this in A instances
>
>  $this->b->doSomething();
>
>  this is the preferred approach, since A and C instances are loosely coupled.
>
>  of course, if you wanted a to 'know' about c then you could do something
>  like this,
>
>  class B {
>   // ..
>   function getC() {
>  return $this->c;
>   }
>  }
>
>  giving you the ability to do this in A instances
>
>  $this->b->getC()->doSomething();
>
>  of course now A's knows about C's and your system is more tightly coupled.
>
>  -nathan
>

Why don't you just do a registry pattern instance then? IE:

class Registry
{
  private satic objs;

  public function __construct()
 {

self::$objs = function_get_args();
  }

  public static function __get($obj)
  {
return self::$objs[$obj];
  }
}

class A
{
...
}

class B
{
...
}

$reg = new Registry( new A(), new B());

Now A and B can access each other through Registry::A and Registry::B

(that code may not function. It's just a general example)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Assigning functions

2008-05-02 Thread Nathan Nobbe
On Fri, May 2, 2008 at 1: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..
>
>  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?!


i cant remember what the term is for it phillip (ill look later), but thats
sort of considered a bad practice..  primarily since c is composed by b and
a doesnt really know about it, then the way a should talk to c is through
b.  so i would create a wrapper method in b (you have many implementation
options here) as a simple example something like this

class B {
  //...
  function doSomething() {
  return $this->c->doSomething();
  }
}

which allows you this in A instances

$this->b->doSomething();

this is the preferred approach, since A and C instances are loosely coupled.

of course, if you wanted a to 'know' about c then you could do something
like this,

class B {
  // ..
  function getC() {
 return $this->c;
  }
}

giving you the ability to do this in A instances

$this->b->getC()->doSomething();

of course now A's knows about C's and your system is more tightly coupled.

-nathan


Re: [PHP] Assigning functions

2008-05-02 Thread Craige Leeder
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

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..
>
>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
>  --
>  PHP General Mailing List (http://www.php.net/)
>  To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Assigning functions

2008-05-02 Thread Philip Thompson
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..


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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php