PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to PHP Subscribe to Newsletters

Advanced PHP Variables and Functions
Pages: 1, 2, 3

Dynamic function creation

Beyond calling functions dynamically, it is also possible create functions dynamically. This can be done through the use of the create_function() function in PHP. The syntax of this function is as follows:



string create_function(string args, string code);

When called, we pass to the function two parameters. The first, args, is a string that contains the code that would normally go between the parentheses of a function declaration. The second parameter code is a string representation of actual PHP code that consists of what normally would be the body of a given function. When the create_function() is called, it creates the function as specified and returns a variable containing the name of that function that you can then use to call the function. Let's take a look at an example:

<?php
  // Normal function definition
  function my_normal_func($foo, $bar) {
    echo "Hi, PHP!";
  }
  // Dynamic function definition
  $func_name = create_function('$foo, $bar', 
               'echo "Hi, PHP!";' );
?>

In this example, we create two functions that are identical in output. In the first example, we declare prior to the script's execution and we name it my_normal_func(). In the second example, we create the function with a call to create_function(). When we execute the script, the function will be created and the name assigned to the function will be stored in the variable $func_name and will have the exact same code and parameters as the function we declared. This process can be useful in a number of circumstances (such as creating callback functions or data validation functions), and it is recommened that you read the manual entry on create_function() available on php.net for more information on how to use this function in everyday coding practice.

Final notes

With PHP, it is possible to create extremely complex, nearly self-creating programs that produce excellent results with a minimal amount of development time. As with almost every powerful tool, improper and careless use of dynamic programming can result in serious security leaks. Never base dynamic code on direct input from the user without first checking to ensure it contains no harmful or malicious code before creating functions or executing commands (particularly dynamic ones) based on it. Until next time -- happy coding!

Also in PHP Foundations:

Using MySQL from PHP, Part 2

Using MySQL from PHP

MySQL Crash Course, Part 3

MySQL Crash Course, Part 2

MySQL Crash Course

John Coggeshall is a a PHP consultant and author who started losing sleep over PHP around five years ago.


Read more PHP Foundations columns.

Return to the PHP DevCenter.




Recommended for You

  1. Cover of Build Your Own Database Driven Website Using PHP and MySQL
    Build Your Own Database Driven Website Using PHP and MySQL
    Print: $39.95
  2. Cover of PHP Pocket Reference
    PHP Pocket Reference
    Print: $9.95
    Ebook: $7.99
  3. Cover of Head First PHP & MySQL
    Head First PHP & MySQL
    Print: $44.99
    Ebook: $35.99
  4. Cover of PHP Cookbook
    PHP Cookbook
    Print: $44.99
    Ebook: $35.99

Tagged Articles

Post to del.icio.us

This article has been tagged:

php

Articles that share the tag php:

Understanding MVC in PHP (477 tags)

The PHP Scalability Myth (123 tags)

The Dynamic Duo of PEAR::DB and Smarty (53 tags)

PHP Form Handling (43 tags)

Very Dynamic Web Interfaces (39 tags)

View All

programming

Articles that share the tag programming:

Rolling with Ruby on Rails (1374 tags)

Very Dynamic Web Interfaces (279 tags)

Ajax on Rails (231 tags)

Understanding MVC in PHP (202 tags)

A Simpler Ajax Path (186 tags)

View All

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com