class creation question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jessevitrone
Forum Newbie
Posts: 15
Joined: Tue Aug 12, 2003 2:42 pm

class creation question

Post 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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

i think i saw something like that on php.net

check the section on classes, it was a user contributed note.
jessevitrone
Forum Newbie
Posts: 15
Joined: Tue Aug 12, 2003 2:42 pm

Post by jessevitrone »

The classes section for php 4 or 5?

Thanks.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Have a look at the factory method: http://phppatterns.com/index.php/article/view/49/1/1
jessevitrone
Forum Newbie
Posts: 15
Joined: Tue Aug 12, 2003 2:42 pm

Post 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.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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
jessevitrone
Forum Newbie
Posts: 15
Joined: Tue Aug 12, 2003 2:42 pm

Post 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.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post 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.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
jessevitrone
Forum Newbie
Posts: 15
Joined: Tue Aug 12, 2003 2:42 pm

Post 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.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

something like:

Code: Select all

//inside the class
function create() {
  return new $this->type;
}
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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;
}
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

sorry, my bad
jessevitrone
Forum Newbie
Posts: 15
Joined: Tue Aug 12, 2003 2:42 pm

Post by jessevitrone »

OK, cool, thanks for the help.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Code: Select all

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