iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 11, 2008
Improving accessibility for motor impaired users
WebDevTips UK
 
May 11, 2008
10 ways to orientate users on your site
WebDevTips UK
 
May 11, 2008
Web Design Clinic - Rros restoration camp 2006
About
 
May 10, 2008
The Moods of Facebook
About
 
May 9, 2008
CSS 1 properties are a great start
About
 
May 9, 2008
Reader Question: How do you get fancy fonts?
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP

Calling functions dynamically 

  Views:    3620
  Votes:    1
by Diego Botello 12/10/03 Rating: 

Synopsis:

Dynamic function calls are a way of calling functions depending on the current situation of the script.
Pages: 
The Article

With the ability to call functions dynamically we can call functions  while the script is running even if we don't know the name of the function at design time. The following example illustrates their use.

<?php

function hello($name)

{

print "Hello $name<br>";

}

$dynamic = "hello";

$dynamic("John");

?> 

This will print "Hello John". As you can see we can call a function adding parenthesis (and the required parameters) to any variable that have a string as a value.

You can also use this feature to call functions within an object like in the following example.
<?php 
class myclass
{
function hello($name)
{
print "Hello $name<br>";
}
} 

$myobject = new myclass();
$dynamic = "hello";
$myobject->$dynamic("John");
?>
 

This will also print "Hello John". In the above example we are  dynamically calling the method hello of our object ($myobject).  Notice that the codes of both examples are almost the same.

Pages: 



 
  Sponsors