Page 1 of 2

class creation question

Posted: Tue Oct 26, 2004 10:42 am
by jessevitrone
I have a method on a base class that I would like to have return a new instance of that class. Is there some way to dynamically create a class like that?

So if A and B both Base, and I call that method from an instance of A, I'd get back an object of type A, and if I call it from an instance of B, I'd get back an object of type B.

I know I can do stuff like this in Java with "this.class.newInstance()", but I don't know if PHP has support for anything like that.

Thanks in advance.

Posted: Tue Oct 26, 2004 4:59 pm
by rehfeld
i think i saw something like that on php.net

check the section on classes, it was a user contributed note.

Posted: Mon Nov 01, 2004 10:48 am
by jessevitrone
The classes section for php 4 or 5?

Thanks.

Posted: Mon Nov 01, 2004 10:53 am
by patrikG
Have a look at the factory method: http://phppatterns.com/index.php/article/view/49/1/1

Posted: Mon Nov 01, 2004 12:01 pm
by jessevitrone
patrikG wrote:Have a look at the factory method: http://phppatterns.com/index.php/article/view/49/1/1
All these seem to just create a new class of a hard coded type. I want to have a similar factory method, but have it in a base class, and all the classes that extend from that could use it.

Code: Select all

class Base {
   var $type = "";

   function create() {
       //what code goes here?
   }
}

class X extends Base {
    function X () {
         $this->type = "X";
    }
}

class Y extends Base {
    function Y () {
         $this->type = "Y";
    }
}

$x = X::create();
$y = Y::create();

$x->type == "X"
$y->type == "Y"
I'm looking for something like that, but I'm not sure what to put in the "create" method on my base class that would return you a new object of the current type.

Thanks.

Posted: Mon Nov 01, 2004 12:05 pm
by patrikG
You mean abstract classes to create a homogenous API? Zend has a good tutorial about it and, of particular interest would be the section about abstract classes: http://www.zend.com/php5/articles/php5-interfaces.php

Posted: Mon Nov 01, 2004 12:34 pm
by jessevitrone
patrikG wrote:You mean abstract classes to create a homogenous API? Zend has a good tutorial about it and, of particular interest would be the section about abstract classes: http://www.zend.com/php5/articles/php5-interfaces.php
Thanks, but I've been staying away from PHP5 since my host doesn't support it yet.

Posted: Mon Nov 01, 2004 12:40 pm
by andre_c
I don't think it's possible because you need the class name of the subclass to be instantiated, and you can't get it without instantiating the class first.
You could do that by making class::type static but i don't know if you can do that in PHP4.

Posted: Mon Nov 01, 2004 12:44 pm
by patrikG
jessevitrone wrote:
patrikG wrote:You mean abstract classes to create a homogenous API? Zend has a good tutorial about it and, of particular interest would be the section about abstract classes: http://www.zend.com/php5/articles/php5-interfaces.php
Thanks, but I've been staying away from PHP5 since my host doesn't support it yet.
You can implement abstract classes in PHP4 as well - probably on the best examples is Vincent Oostind's Eclipse library: http://sourceforge.net/projects/eclipselib

Especially the db-wrapper and the iterator-classes would be of interest, I reckon.

Posted: Mon Nov 01, 2004 12:45 pm
by jessevitrone
andre_c wrote:I don't think it's possible because you need the class name of the subclass to be instantiated, and you can't get it without instantiating the class first.
You could do that by making class::type static but i don't know if you can do that in PHP4.
Even if I had the class type, how would I instantiate a new class with some kind of a var saying the class type?

Thanks.

Posted: Mon Nov 01, 2004 12:47 pm
by andre_c
something like:

Code: Select all

//inside the class
function create() {
  return new $this->type;
}

Posted: Mon Nov 01, 2004 12:50 pm
by patrikG
andre_c wrote:something like:

Code: Select all

//inside the class
function create() {
  return new $this->type;
}
Make sure you use references when returning objects, otherwise you bloat code held in the server's memory unnecessarily. E.g.:

Code: Select all

//inside the class
function create() {
  return new &$this->type;
}

Posted: Mon Nov 01, 2004 12:51 pm
by andre_c
sorry, my bad

Posted: Mon Nov 01, 2004 1:43 pm
by jessevitrone
OK, cool, thanks for the help.

Posted: Mon Nov 01, 2004 3:53 pm
by Christopher

Code: Select all

//inside the class
function create() {
  $type = get_class($this);
  return new $type;
}