473,498 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 17 '05 #1
2 3041
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
Jul 17 '05 #2
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!
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2117
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
4
1622
by: Tadeusz S. | last post by:
Hello In Pascal, I use constructions like that var SomeVar = object SomeVar2 = object end; end; etc., so I could put my object into another
7
1345
by: Nick Beenham | last post by:
Can anyone give me a tip on how to pass a value from a class to a windows form. e.g. adding text to a textbox. public class AnotherClass { //usual class code public void AddText() {
4
1519
by: Typpo | last post by:
Hi, This question is somewhat basic. Is it possible to call a method in frmMain, using another class? Here's a snippet: partial class frmMain : Form { private void MessageThis(String text)...
6
2103
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
4
1281
by: D | last post by:
I was reviewing these vb.net classes which deal with user logins, logouts, cookies etc etc. I noticed that in the Page_Load function of a user control the code calls into another library like so...
2
6061
by: rhamlin | last post by:
I'm working on a class. When instantiating I pass the variable name referencing the class (vw) and the page object where the class will display (view). In this class I create a DIV. I also...
19
1900
by: Juha Nieminen | last post by:
Assume I have a class Base, and then this: struct Derived: public Base { AnotherClass member; }; Also assume that I'm allocating an object of type 'Derived' with a custom allocator and...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4590
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.