defining a class/instantiating object inside a method of anotherclass

This is a discussion on defining a class/instantiating object inside a method of anotherclass within the PHP Language forums, part of the PHP Programming Forums category; Ok, here's what I'm trying to do. I have a class. Inside the class I have a method ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-19-2004
Lüpher Cypher
 
Posts: n/a
Default defining a class/instantiating object inside a method of anotherclass

Ok, here's what I'm trying to do.
I have a class. Inside the class I have a method that reads a text file.
In the text file I have a class name and a file name where that class is
defined. Now, I need to instantiate an object of that class, the problem
being that at the time I find out the class name and the file name, I'm
inside a method of another class. Well, let me just type up a little
example:

class MainClass {
$obj = null;
...
function init() {
$lines = file($some_file);
... // parsing
$kv = explode(":",$line);
$className = $kv[0];
$classFile = $kv[1];

// somehow define className that's in classFile

$this->obj = new $$className();
}
}

I don't remember exactly how it was I used a variable with new, I think
it was $$, but that's not the problem. The problem is that I don't need
to include the class definition file inside a function, I just need to
define a class using what's inside that file.
Taking the whole thing outside of the class definition is not going to
work - suppose I have many subclasses of this class, each with its own
file, then for every class I'd have to provide a separate function
outside the class definition to read the file.
Think of it as creating a tree of objects where children objects' class
names are taken out into a separate text file, so that at any time it
would be easy to change the set of succeeding children without changing
the parent class implementation.
Any ideas? :)

Lüph
Reply With Quote
  #2 (permalink)  
Old 10-19-2004
Berislav Lopac
 
Posts: n/a
Default Re: defining a class/instantiating object inside a method of another class

On Tue, 19 Oct 2004 04:07:11 GMT, Lüpher Cypher wrote:

> Taking the whole thing outside of the class definition is not going to
> work - suppose I have many subclasses of this class, each with its own
> file, then for every class I'd have to provide a separate function
> outside the class definition to read the file.


No, just include the class definition file anywhere, even inside the
function block. As PHP is parsed and compiled before it's executed, class
definitions are valid for whole program, regardless of where they are
defined.

Berislav
Reply With Quote
  #3 (permalink)  
Old 10-19-2004
Lüpher Cypher
 
Posts: n/a
Default Re: defining a class/instantiating object inside a method of anotherclass

Berislav Lopac wrote:
> On Tue, 19 Oct 2004 04:07:11 GMT, Lüpher Cypher wrote:
>
>
>>Taking the whole thing outside of the class definition is not going to
>>work - suppose I have many subclasses of this class, each with its own
>>file, then for every class I'd have to provide a separate function
>>outside the class definition to read the file.

>
>
> No, just include the class definition file anywhere, even inside the
> function block. As PHP is parsed and compiled before it's executed, class
> definitions are valid for whole program, regardless of where they are
> defined.
>


Cool, I didn't know that. Thanks!
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:51 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0