Re: [PHP] $this = new Class();

2009-05-07 Thread Richard Quadling
2009/5/7 Richard Quadling rquadl...@googlemail.com:
 2009/4/30 Olivier Lalonde olalo...@gmail.com:
 Hi all,

 Since I can't do $this = new Class(); within my class (it gives an
 error), I was looking for ways to get the same result by other means.

 I am actually working on an ORM and trying to implement lazy loading.

 $book = $orm-getBook('id'); // returns an Orm object
 $book-load();

 // $book should now be a Book instead instead of an Orm instance

 Of course, I oversimplified the problem.  $book = $orm-getBook('id');
 doesn't return a Book instance right ahead because it is a chained
 method (i.e. $orm-getBook()-where(...)-prefetch(...)-etc
 Therefore, it _has_ to return an Orm instance.

 Now, why not simply add -load() at the end of the chain? Because it
 adds an extra step for developers that doesn't bring meaningful
 information. Instead of doing $book = $orm-getBook('id');, it would
 mean having to do $book = $orm-getBook('id')-load(); (which is
 longer to type :p). That's why I wanted to implement lazy loading.

 $book = $dorm-getBook('id');
 echo $book-title; // title should be trapped by __set() and it should
 dynamically replace $book by an actual Book instance

 I tried doing the following, but PHP doesn't allow it:

 class A {
  public function transform() {
    $this = new B();
  }
 }

 class B {}

 $var = new A();
 $var-transform();

 This is not currently supported by PHP and I was wondering if there
 was anyway of getting around the problem, that doesn't involve
 1) passing $var to the A class i.e:$var-var = $var;
 2) looping $GLOBALS[]
 3) using __call,__get and __set to proxy everything to the actual Book object

 PS1: don't lecture me about how I'm doing this all wrong. I've looked
 at the problem from every possible angle and this is the only
 solution.
 PS2: Another alternative would be to subclass the Orm object with
 Book, (class Orm extends Book {}), overload all properties/methods so
 we can catch when to load the object... but that would be an extreme
 pain in the ass.
 PS3: Another alternative would be to have a parameter that
 enables/disables chaining.
 $dorm-getBook('id', true); // chain (you now have to add -load() at
 the end of the chain)
 $dorm-getBook('id', false); // dont chain, this returns a Book instance

 The point of all this is to keep the most friendly interface !

 Cheers,
 Olivier

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



 In $orm-getBook('id') should be something similar to ...

 $book = new ClassBook('id');
 $book-load;
 return $book;

 surely?

 --
 -
 Richard Quadling
 Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731
 Standing on the shoulders of some very clever giants!


Oops.

$book-load();

Sorry.

-- 
-
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731
Standing on the shoulders of some very clever giants!

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



Re: [PHP] $this = new Class();

2009-05-07 Thread Richard Quadling
2009/4/30 Olivier Lalonde olalo...@gmail.com:
 Hi all,

 Since I can't do $this = new Class(); within my class (it gives an
 error), I was looking for ways to get the same result by other means.

 I am actually working on an ORM and trying to implement lazy loading.

 $book = $orm-getBook('id'); // returns an Orm object
 $book-load();

 // $book should now be a Book instead instead of an Orm instance

 Of course, I oversimplified the problem.  $book = $orm-getBook('id');
 doesn't return a Book instance right ahead because it is a chained
 method (i.e. $orm-getBook()-where(...)-prefetch(...)-etc
 Therefore, it _has_ to return an Orm instance.

 Now, why not simply add -load() at the end of the chain? Because it
 adds an extra step for developers that doesn't bring meaningful
 information. Instead of doing $book = $orm-getBook('id');, it would
 mean having to do $book = $orm-getBook('id')-load(); (which is
 longer to type :p). That's why I wanted to implement lazy loading.

 $book = $dorm-getBook('id');
 echo $book-title; // title should be trapped by __set() and it should
 dynamically replace $book by an actual Book instance

 I tried doing the following, but PHP doesn't allow it:

 class A {
  public function transform() {
    $this = new B();
  }
 }

 class B {}

 $var = new A();
 $var-transform();

 This is not currently supported by PHP and I was wondering if there
 was anyway of getting around the problem, that doesn't involve
 1) passing $var to the A class i.e:$var-var = $var;
 2) looping $GLOBALS[]
 3) using __call,__get and __set to proxy everything to the actual Book object

 PS1: don't lecture me about how I'm doing this all wrong. I've looked
 at the problem from every possible angle and this is the only
 solution.
 PS2: Another alternative would be to subclass the Orm object with
 Book, (class Orm extends Book {}), overload all properties/methods so
 we can catch when to load the object... but that would be an extreme
 pain in the ass.
 PS3: Another alternative would be to have a parameter that
 enables/disables chaining.
 $dorm-getBook('id', true); // chain (you now have to add -load() at
 the end of the chain)
 $dorm-getBook('id', false); // dont chain, this returns a Book instance

 The point of all this is to keep the most friendly interface !

 Cheers,
 Olivier

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



In $orm-getBook('id') should be something similar to ...

$book = new ClassBook('id');
$book-load;
return $book;

surely?

-- 
-
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731
Standing on the shoulders of some very clever giants!

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



Re: [PHP] creating new class from wrapper class (OOP question)

2006-01-19 Thread Jochem Maas

Henrik Gemal wrote:

In a image gallery I have to class'es:

class GPicFilePicture extends GPicFileType
class GPicFileMovie extends GPicFileType

both of them are based on:

abstract class GPicFileType

In my code I need to create a new GPicFilePicture. To avoid duplicated
code I've create a wrapper class:

class GPicFile

that does something like this:

if (fileextension == jpg)
  return new GPicFilePicture();
else
  return new GPicFileMovie();


I assume the above is in your constructor, and running php5.
if so then what your returning is not being returned... php is returning
the GPicFile object you just asked for instead.

try something like:

GPicFile
{
function __construct($f)
{
if (fileextension == jpg)
$this-decoration = new GPicFilePicture();
else
$this-decoration = new GPicFileMovie();
}

function getFileDate()
{
$this-decoration-getFileDate()
}
}

which I believe is called a decorator pattern (don't quote me.)



so my PHP code looks like:
$file = new GPicFile($filename);

getFileDate() is implemented in both GPicFilePicture and GPicFileMovie.

Now I try to do:
$file-getFileDate();

I get an error saying:
Call to undefined method GPicFile::getFileDate()

Where am I going wrong?



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



Re: [PHP] creating new class from wrapper class (OOP question)

2006-01-19 Thread Richard Lynch
On Tue, January 17, 2006 8:56 am, Henrik Gemal wrote:
 In a image gallery I have to class'es:

 class GPicFilePicture extends GPicFileType
 class GPicFileMovie extends GPicFileType

 both of them are based on:

 abstract class GPicFileType

 In my code I need to create a new GPicFilePicture. To avoid duplicated
 code I've create a wrapper class:

 class GPicFile

 that does something like this:

 if (fileextension == jpg)
return new GPicFilePicture();
 else
return new GPicFileMovie();

This isn't in the constructor, is it?

You would need to do something like:

class GPicFile {
  var $implementor = NULL;

  function implementor($filename){
if (fileextension == 'jpg')
  $this-implementor = new GPicFilePicture();
else
  $this-implementor = new GPicFileMovie();
  }

  function getFileDate(){
return $this-implementor-getFileDate();
  }
}

You can't have a wrapper class that returns one class or another.

You could have a FUNCTION that would return one or the other, and then
you'd have to make sure you handled either kind of object, but not a
class that pretends to be two different kinds of objects.

 so my PHP code looks like:
 $file = new GPicFile($filename);

 getFileDate() is implemented in both GPicFilePicture and
 GPicFileMovie.

 Now I try to do:
 $file-getFileDate();

 I get an error saying:
 Call to undefined method GPicFile::getFileDate()

You can't make really make GPicFile object sometimes be a
GPicFilePicture and sometimes be a GPicFileMovie...

You could sort of do this if PHP had multiple inheritence, but it
doesn't.

There might be something in the fancy new PHP5 stuff about interface
and whatnot that would be more elegant than the hack above.

But you're on your own for that.

-- 
Like Music?
http://l-i-e.com/artists.htm

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