Jump to content

Creating objects at runtime using the class name

- - - - -

  • Please log in to reply
1 reply to this topic

#1
rhossis

rhossis

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
Hi,

I am writing a code for accounting system where the main accounting class is to load a class that contains a schedule for the firms reporting cycle with information such as fiscal year, tax schedules. The reporting cycle class does do not extend the main accounting class, but am trying to implement a pattern where the reporting cycle classes names can register be registered with the main accounting class on the constructor and it instantiates them and controls data access to them.

Is it possible to instantiate an object using the class name, maybe using a concept similar to variable functions where one would do

function a_function(){

    echo "I am a function";

}

$func="a_function";

$func();


Thanks in advance

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
You do the same with a class


class Test {

}

$MyClass = "Test";

$MyObj = new $MyClass();


__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users