oop - PHP: How to instantiate a class with arguments from within another class -
I am in such situations where I have to instantify a class with reasoning from within another class example. Here's the prototype:
// test.php class test {function __construct ($ a, $ b, $ c) {echo $ a. '& Lt; Br / & gt; '; Echo $ B '& Lt; Br / & gt; '; Countercurrent $ c '& lt; Br / & gt; '; }}
Now, I need to inspire the above class using the class's CLS function:
class Myclass {function cls ($ File_name, $ args = array ()) Include {$ file_name ".php"; If (isset ($ args)) {// This is where the problem can be, I have to pass several arguments in the form of exam square $ class_instance = new $ file_name ($ args); } And {$ class_instance = new $ file_name (); } Return $ Class_instance; }}
Now when I pass arguments to do this, I try to make an example of the test class:
$ myclass = new Myclass; $ Test = $ myclass- & gt; CLS ('test', array ('a1', 'b2', 'c3'));
returns this error: missing arguments 1 and 2; Only the first argument is passed.
This works fine, if I instantiate that class, there is no argument in its constructor function.
For experienced PHP developers, there should not be any problems above. help please.
Thank you
You need reflection
If (count ($ args) == 0) $ obj = new $ className; Other {$ r = New Reflection ($ className); $ Obj = $ r- & gt; New instant ARGs ($ Args); }
Comments
Post a Comment