Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why $this->module = new $loadModule;

Status
Not open for further replies.

wolf73

Programmer
Feb 12, 2006
93
CA
LIttle of topic. I got this code from php manual.

<?php

class mainClass
{
/*
* Class loading function.
*
*
*
*
*/

var $module;

function LoadClass($loadModule)
{
$this->module = new $loadModule; // this is much better
}

function RunFunction($FunctionName)
{
$this->module->$FunctionName();
}

function ListFunctions()
{

}

// function out()
//{
// $this->module->out();
// }
}

class subclass
{
function out()
{
echo "Worked";
}

function NewFunction()
{
echo "<br>Good Lord<br>";
}

function ThirdFunction()
{
echo "<br>eh....<br>";

}
}

echo "Start of class manulapations<br>";
//$loadModule = new subclass;
$mainclass=new mainClass;
$mainclass->LoadClass("subclass");
//$mainclass->out();
$mainclass->RunFunction('NewFunction');
//$mainclass->ThirdFunction();
echo "End of class manulapation<br>";
?>

*******

I don't understand what he is doing in first function

$this->module = new $loadModule; // this is much

and second function

$this->module->$FunctionName();
 
PHP allows you to use string variables to create new objects or call functions rather than writing out the function or class name. That's what these examples are doing. The object that's created and the function that's called depends on the value of the variable. So, for example, this
Code:
$loadModule = "ClassFoo";
$FunctionName = "funcBar";
$this->module = new $loadModule;
$this->module->$FunctionName();
is equivalent to this
Code:
$this->module = new ClassFoo;
$this->module->funcBar();
 
thanks a lot you explained it really well
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top