473,770 Members | 1,677 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(":",$li ne);
$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 3060
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
2160
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 access module that creates a large disconnected dataset from a database. I want to pass that dataset back to the calling program. And then perhaps I want to send that dataset to another class module. At first it seems that the "object oriented"...
4
1636
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
1358
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
1538
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) { MessageBox.Show(text);
6
2119
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 responsible for instantiating the orders class? Would it be the ui layer or the master class in the business layer? thanks,
4
1300
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 Call AnotherClassLibrary.AnotherClass.UserLogin(userId) I do not understand how this code can call this function without first creating an instance of the AnotherClass.
2
6078
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 place a DIV inside the first which will act as a button and when clicked will run code that's also inside the class. To create the button DIV I use the addBtn function inside the class.
19
1928
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 that, for whatever reason, I don't want to call the constructor of 'Derived', but instead I want to build the object by
0
9602
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10071
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9882
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
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 we have to send another system
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.