473,586 Members | 2,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP dynamic class instantiation like Java

I've leafed through the docs and user comments on php.net, but haven't
been able to find anything related to this.

Is there a way to dynamically instantiate a class, like you can in
Java with class.forName() ?
example:

$myclass = "Automobile ";
$func = "drive";

$myInstance = new $myclass;
call_user_metho d ($func, $myInstance); // i know this is deprecated.
Jul 17 '05 #1
2 23150
Create a factory-like method that will return an instance of the class
and use call_user_func( ) with an array as the first parameter
(containing the class name and your factory method)

class foo
{
function factory()
{
return new foo();
}
}

$obj = call_user_func( array('foo', 'factory') );

--
Louis-Philippe Huberdeau

Roger Vandervort wrote:
I've leafed through the docs and user comments on php.net, but haven't
been able to find anything related to this.

Is there a way to dynamically instantiate a class, like you can in
Java with class.forName() ?
example:

$myclass = "Automobile ";
$func = "drive";

$myInstance = new $myclass;
call_user_metho d ($func, $myInstance); // i know this is deprecated.


Jul 17 '05 #2
Louis-Philippe Huberdeau <lp*********@sy mpatico.ca> wrote in message news:<qF******* ************@ne ws20.bellglobal .com>...
Create a factory-like method that will return an instance of the class
and use call_user_func( ) with an array as the first parameter
(containing the class name and your factory method)

class foo
{
function factory()
{
return new foo();
}
}

$obj = call_user_func( array('foo', 'factory') );

--
Louis-Philippe Huberdeau

Roger Vandervort wrote:
I've leafed through the docs and user comments on php.net, but haven't
been able to find anything related to this.

Is there a way to dynamically instantiate a class, like you can in
Java with class.forName() ?
example:

$myclass = "Automobile ";
$func = "drive";

$myInstance = new $myclass;
call_user_metho d ($func, $myInstance); // i know this is deprecated.

Actually, I did determine that this does work:

class Automobile {
function myFunction($ech oString) { echo 'echoing
Automobile:'.$e choString; }
}

$myClass = "Automobile ";
$func = "myFunction ";
$param = 'Vroom...Vroom' ;
$myInstance = new $myClass;
call_user_funct ion(array(&$myC lass,$func),$pa ram); // will print
'echoing Automobile:Vroo m...Vroom';

Not sure which versions support this though.
Jul 17 '05 #3

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

Similar topics

0
1557
by: Shailesh | last post by:
If I'm not mistaken, C++ doesn't have support for dynamic class members. I'm considering if such a facility would be useful, and what method would make a good workaround. I have a generic buffer class, which can have one or more cursor classes attached to it. Right now, they are both separate classes, and I use them as follows: class...
1
3284
by: Jim Witt | last post by:
How can a class inside a class (who is already instantiated) be instantiated from a string whos value is the name of the class? Thanks!
1
2131
by: andrew queisser | last post by:
I've been trying to dynamically create a class DevT that's derived from a generic base GenBase<T>. It doesn't seem to work. I'm attaching a code sample below that illustrates the problem. CreateType() fails when the base class is a parametrized class, as in DevT : GenBase<int>. CreateType() works if the base class is not parametrized, even...
10
2666
by: Ognen Duzlevski | last post by:
Hi, I have a "language definition" file, something along the lines of: page :: name : simple caption : simple function : complex function :: name : simple code : simple
5
2321
by: Slant | last post by:
Here's a question that most will have different answers to. I'm just dying to find a solution that seems halfway automated!! There really are two seperate issues which might be answered by the same answer. Web App: - constants.php begin - define("DB_HOST", "locahost"); .... to include the standard connection info define("DB_NAME",...
6
13755
by: Bora Ji | last post by:
Please help me to creating dynamic VARIABLE in java, with predefined name.
0
1020
by: Jeff Tchang | last post by:
Odd issue I am having with class instantiation on Python 2.5.2 (Windows). I have a custom module with a few classes in it. The module is named SAML.py. There is a copy of it in C:\Python25\Lib\site-packages\SAML.py. Basically when I try to run a python file that tries to create an instance of the class Subject I get this error:...
0
1003
by: Steve Holden | last post by:
Jeff Tchang wrote: Look for assignments to "SAML" in your code, or a class definition of something called SAML. That message should read "module object "SAML' has no attribute 'Subject'". If SAML isn't a module, why not? regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC ...
0
1160
by: John Krukoff | last post by:
On Tue, 2008-11-18 at 10:45 -0800, Jeff Tchang wrote: Random related question. If you're writing a SAML implementation have you found an XML Signature implementation that works reliably from python? I've had a hell of a time finding something that doesn't segfault and is interoperable with .NET. As far as your problem goes, I would guess...
0
8338
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...
1
7954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8215
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...
0
6610
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5390
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2345
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
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
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...

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.