Re: [PHP] Container or Calling Class

2009-05-24 Thread Eddie Drapkin
No problem mate :)

As a general rule, it seems that PHP just copied Java's OOP model - and
outside of the quirks in the way it works internally - it's a pretty damn
good implementation of object orientation.  THe only issue I have with it is
that it's not possible to lose the procedural bootstrapper :(

On Sun, May 24, 2009 at 11:24 PM, phphelp -- kbk wrote:

> Thanks, guys, for the discussion. This is nothing compelling for me, just
> fooling around with the model. My first OOP language many-a-year ago had
> this ability. "Parent" designated containment, not (as I believe, too, is
> more "correct") inheritance hierarchy, like PHP. I and have never had the
> need to call a "containing" function in PHP, but I was exploring the best
> way to accomplish a new task the other day, and that kind of architecture
> seemed like a possibility.
>
> In fact, probably just a bad idea.
>
> (I see so much of that in this list: "I need to..." followed by a
> description of some bizarre construct. I want to scream "No! You have
> painted yourself into a corner! Test your assumptions, or go back 5 steps
> and start over.")
>
> Thanks again
>
> Ken
>
>
> On May 24, 2009, at 9:38 PM, Eddie Drapkin wrote:
>
>  That's containment, not inheritence, must have misread the email.  Oops :)
>>
>> The "easiest" way to do this would be something like:
>>
>> class contrived {
>> private $parent;
>> private $otherparent;
>>
>> public function __call($func, $params) {
>> if(is_callable(array($this->parent, $func))
>> call_user_func_array(array($this->parent, $func), $params)
>> elseif(is_callable(array($this->otherParent, $func))
>> call_user_func_array(array($this->otherparent, $func), $params)
>> }
>>
>> }
>>
>> This is the most "elegant" way to accomplish the same thing, but it makes
>> some assumptions, like you're willing to chain if-elseif's together (or
>> store all contained instances in an array) and that you don't ever have
>> overlapping method names (ie contrived.foo and parent.foo both exist).
>>  Also, it's really, really obnoxiously slow.  __call() is hardly fast and
>> call_user_func_array is no different.
>>
>> On Sun, May 24, 2009 at 7:52 PM, Nathan Rixham  wrote:
>> Eddie Drapkin wrote:
>> You can call methods from a classes's parents like so
>> class foo {
>>
>> protected method bar() {
>> echo "in foo!";
>> }
>>
>> }
>>
>> class foobar extends foo {
>>
>> public function bar() {
>> parent::bar();
>> }
>> }
>>
>> $fb  = new foobar();
>> $fb->bar(); will output "in foo!";
>>
>>
>> wrong way round.. he's asking for:
>>
>> // note no extends
>> class foobar {
>>
>>  public function bar() {
>>   $this->foo = new Foo();
>>   $this->foo->bar();
>>  }
>>
>>  public function poo() {
>>echo "call me if you can";
>>  }
>>
>> }
>>
>> // and the impossible
>> class foo {
>>
>>  public method bar() {
>>   foobar->poo(); // call the containing class
>>  }
>>
>> }
>>
>> in a dom or as3 or suchlike this would be
>>
>> this.parent.poo();
>>
>> but we have not this->parent
>>
>> note: note parent::poo() which is effectively super.poo() 
>>
>> so he can only inject the container/parent
>>
>> >
>> class foobar {
>>
>>  private $foo;
>>
>>  public function bar() {
>>   $this->foo = new Foo( $this );
>>   $this->foo->bar();
>>  }
>>
>>  public function poo() {
>>echo "call me if you can";
>>  }
>>
>> }
>>
>> // and the Possible
>> class foo {
>>
>>  private $parent;
>>
>>  public function __construct( $parent ) {
>>   $this->parent = $parent;
>>  }
>>
>>  public method bar() {
>>   $this->parent->poo(); // call the containing class method poo
>>  }
>>
>> }
>>
>>
>


Re: [PHP] Container or Calling Class

2009-05-24 Thread Eddie Drapkin
That's containment, not inheritence, must have misread the email.  Oops :)

The "easiest" way to do this would be something like:

class contrived {
private $parent;
private $otherparent;

public function __call($func, $params) {
if(is_callable(array($this->parent, $func))
call_user_func_array(array($this->parent, $func), $params)
elseif(is_callable(array($this->otherParent, $func))
call_user_func_array(array($this->otherparent, $func), $params)
}

}

This is the most "elegant" way to accomplish the same thing, but it makes
some assumptions, like you're willing to chain if-elseif's together (or
store all contained instances in an array) and that you don't ever have
overlapping method names (ie contrived.foo and parent.foo both exist).
Also, it's really, really obnoxiously slow.  __call() is hardly fast and
call_user_func_array is no different.

On Sun, May 24, 2009 at 7:52 PM, Nathan Rixham  wrote:

> Eddie Drapkin wrote:
>
>> You can call methods from a classes's parents like so
>> class foo {
>>
>> protected method bar() {
>> echo "in foo!";
>> }
>>
>> }
>>
>> class foobar extends foo {
>>
>> public function bar() {
>> parent::bar();
>> }
>> }
>>
>> $fb  = new foobar();
>> $fb->bar(); will output "in foo!";
>>
>>
> wrong way round.. he's asking for:
>
> // note no extends
> class foobar {
>
>  public function bar() {
>$this->foo = new Foo();
>$this->foo->bar();
>  }
>
>  public function poo() {
> echo "call me if you can";
>  }
>
> }
>
> // and the impossible
> class foo {
>
>  public method bar() {
>foobar->poo(); // call the containing class
>  }
>
> }
>
> in a dom or as3 or suchlike this would be
>
> this.parent.poo();
>
> but we have not this->parent
>
> note: note parent::poo() which is effectively super.poo() 
>
> so he can only inject the container/parent
>
> 
> class foobar {
>
>  private $foo;
>
>  public function bar() {
>$this->foo = new Foo( $this );
>$this->foo->bar();
>  }
>
>  public function poo() {
> echo "call me if you can";
>  }
>
> }
>
> // and the Possible
> class foo {
>
>  private $parent;
>
>  public function __construct( $parent ) {
>$this->parent = $parent;
>  }
>
>  public method bar() {
>$this->parent->poo(); // call the containing class method poo
>  }
>
> }
>


Re: [PHP] Container or Calling Class

2009-05-24 Thread Nathan Rixham

Eddie Drapkin wrote:

You can call methods from a classes's parents like so
class foo {

protected method bar() {
echo "in foo!";
}

}

class foobar extends foo {

public function bar() {
parent::bar();
}
}

$fb  = new foobar();
$fb->bar(); will output "in foo!";



wrong way round.. he's asking for:

// note no extends
class foobar {

  public function bar() {
$this->foo = new Foo();
$this->foo->bar();
  }

  public function poo() {
 echo "call me if you can";
  }

}

// and the impossible
class foo {

  public method bar() {
foobar->poo(); // call the containing class
  }

}

in a dom or as3 or suchlike this would be

this.parent.poo();

but we have not this->parent

note: note parent::poo() which is effectively super.poo() 

so he can only inject the container/parent

foo = new Foo( $this );
$this->foo->bar();
  }

  public function poo() {
 echo "call me if you can";
  }

}

// and the Possible
class foo {

  private $parent;

  public function __construct( $parent ) {
$this->parent = $parent;
  }

  public method bar() {
$this->parent->poo(); // call the containing class method poo
  }

}

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



Re: [PHP] Container or Calling Class

2009-05-24 Thread Eddie Drapkin
You can call methods from a classes's parents like so
class foo {

protected method bar() {
echo "in foo!";
}

}

class foobar extends foo {

public function bar() {
parent::bar();
}
}

$fb  = new foobar();
$fb->bar(); will output "in foo!";


On Sun, May 24, 2009 at 3:58 PM, Nathan Rixham  wrote:

> Stuart wrote:
>
>> 2009/5/24 phphelp -- kbk :
>>
>>> If so, can the bar_handler->bar_toast() function call a function in the
>>> container class (foo_handler)? "Parent" is used in some OOP languages for
>>> this type of hierarchy, but not PHP. I have fooled around with the scope
>>> resolution operator, but either that doesn't do it or I can't nail the
>>> syntax.
>>>
>>> Anyone care to illuminate this for me?
>>>
>>
>> I've never heard of this being possible in any language, never mind
>> PHP. The parent keyword you mention refers to the parent class not the
>> container, and works just as well in PHP as in other languages. Any
>> OOP implementation that allows a class to call functions in a
>> container class would be breaking one of the cardinal rules of OOP.
>>
>> -Stuart
>>
>>
> no offence but it smacks a bit of bad design tbh - however one possibility
> is to inject a reference to the container class instance in to the child
>
> $this->bar_handler = new bar_handler( $this );
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


Re: [PHP] Container or Calling Class

2009-05-24 Thread Nathan Rixham

Stuart wrote:

2009/5/24 phphelp -- kbk :

If so, can the bar_handler->bar_toast() function call a function in the
container class (foo_handler)? "Parent" is used in some OOP languages for
this type of hierarchy, but not PHP. I have fooled around with the scope
resolution operator, but either that doesn't do it or I can't nail the
syntax.

Anyone care to illuminate this for me?


I've never heard of this being possible in any language, never mind
PHP. The parent keyword you mention refers to the parent class not the
container, and works just as well in PHP as in other languages. Any
OOP implementation that allows a class to call functions in a
container class would be breaking one of the cardinal rules of OOP.

-Stuart



no offence but it smacks a bit of bad design tbh - however one 
possibility is to inject a reference to the container class instance in 
to the child


$this->bar_handler = new bar_handler( $this );

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



Re: [PHP] Container or Calling Class

2009-05-24 Thread Stuart
2009/5/24 phphelp -- kbk :
> Hey -- folks -- -- -
>
> I am trying to figure out one aspect of PHP's object model.
>
> Is it good practice to have a class be a property of another class? In other
> words:
>
> class foo_handler {
>        function add_bar() {
>                include bar_class.inc;
>                $this->bar_handler = new bar_handler;
>        }
>
> }
>
> So one would have language in a foo_handler function like:
>
> $this->bar_handler->bar_toast("Rye",$settings);

This is pretty common practice. However, it's worth pointing out that
putting the include statement there will prevent a number of potential
performance improvements on the part of the PHP compiler and any
bytecode caches that might be active.

> If so, can the bar_handler->bar_toast() function call a function in the
> container class (foo_handler)? "Parent" is used in some OOP languages for
> this type of hierarchy, but not PHP. I have fooled around with the scope
> resolution operator, but either that doesn't do it or I can't nail the
> syntax.
>
> Anyone care to illuminate this for me?

I've never heard of this being possible in any language, never mind
PHP. The parent keyword you mention refers to the parent class not the
container, and works just as well in PHP as in other languages. Any
OOP implementation that allows a class to call functions in a
container class would be breaking one of the cardinal rules of OOP.

-Stuart

-- 
http://stut.net/

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