SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
-
Feb 4, 2004, 09:20 #1
- Join Date
- Jul 2003
- Location
- up in the clouds
- Posts
- 51
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
object delegate (was [$this = $that; and PHP4+5])
Hi,
I learn from #php.internals that $this = $that; will raise a fatal error with PHP5.
PHP Code:class MyClass
{
function MyClass($type, $params)
{
if ($type='A') {
$this = new A($params);
} else {
$this = new B($params);
}
}
}
class A
{
function A($params) {}
function doThis() {}
function doThat() {}
}
class B
{
function B($params) {}
function doThis() {}
function doThat() {}
}
How can the MyClass preserve its behaviour without changing the API *and* making it work under both PHP4 and PHP5?
Basically, MyClass could act as a proxy, but I don't like the idea of redirecting every single method to the proxied class:
PHP Code:class MyClass
{
var $class = null;
function MyClass($type, $params)
{
if ($type='A') {
$this->class = new A($params);
} else {
$this->class = new B($params);
}
}
function doThis() {
return $this->class->doThis();
}
function doThat() {
return $this->class->doThat();
}
}
Last edited by duff_beer; Feb 6, 2004 at 04:36.
-
Feb 4, 2004, 09:26 #2
- Join Date
- Jun 2003
- Location
- Iowa, USA
- Posts
- 3,749
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Have a factory method return the right object instead?
Jason Sweat ZCE - jsweat_php@yahoo.com
Book: PHP Patterns
Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
Detestable (adjective): software that isn't testable.
-
Feb 4, 2004, 09:48 #3
- Join Date
- Jul 2003
- Location
- up in the clouds
- Posts
- 51
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi Jason, thanks for you reply.
Originally Posted by sweatje
PHP Code:$myClass = new MyClass('A', $params);
echo $myClass->property;
They don't need to know there are two subclasses (A and B), and they should be able to access A or B properties like they were properties of MyClass.
Could you write a small code snippet to illustrate your point?
Thanks a lot
-
Feb 4, 2004, 12:23 #4
- Join Date
- Jun 2003
- Location
- Iowa, USA
- Posts
- 3,749
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You are correct. If you are not allowed to touch $this, then using a factory method will require an API change.
-
Feb 6, 2004, 05:01 #5
- Join Date
- Jul 2003
- Location
- up in the clouds
- Posts
- 51
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Object Delegates
Thanks Jason.
Slightly changing the post topic, I'd like to collect ideas on how to implement an object delegate in PHP4, without using extensions not in the default package.
Returning to the example in the first post, "MyClass" is a clear example of a delegate object. It just returns an object of the desired class.
To make it clearer, let's change the above example to this one:
PHP Code:class MyClass
{
function MyClass($type, $params)
{
if (!@include_once($type.'.php')) {
$type = 'A';
include_once('A.php');
}
$this = new $type($params);
}
}
1) a lot of code is needed just to proxy method calls:
PHP Code:function doThis() {
return $this->class->doThis();
}
So, the final question is:
How can we define a delegate class in PHP4?
or, if you prefer:
How can we define a "lazy proxy" without using the "__call()" function?
-
Feb 6, 2004, 12:12 #6
- Join Date
- Jun 2003
- Location
- Iowa, USA
- Posts
- 3,749
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Might want to take a look at Marcus's post (#5) in this thread.
-
Feb 6, 2004, 13:47 #7
- Join Date
- Jul 2003
- Location
- up in the clouds
- Posts
- 51
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
had seen that, but it relies on "__call()" and "overload()"...
Bookmarks