PHP: Funkce pro pr�ci s funkcemi - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<ftp_systypecall_user_func_array>
view the version of this page
Last updated: Thu, 15 Jul 2004

XXXV. Funkce pro pr�ci s funkcemi

�vod

V�echny tyto funkce pokr�vaj� r�zn� aspekty pr�ce s funkcemi.

Po�adavky

Tyto funkce jsou k dispozici jako sou��st standardn�ho modulu, kter� je v�dy dostupn�.

Instalace

K pou��v�n� t�chto funkc� nen� t�eba ��dn� instalace, jsou sou��st� j�dra PHP.

Konfigurace b�hu

Toto roz���en� nem� definov�no ��dn� konfigura�n� direktivy.

P�eddefinovan� konstanty

Toto roz���en� nem� definov�ny ��dn� konstanty.

Obsah
call_user_func_array --  Call a user function given with an array of parameters
call_user_func -- Zavolat u�ivatelskou funkci ur�enou prvn�m argumentem
create_function -- Vytvo�it anonymn� (lambda-style) funkci
func_get_arg -- Vr�tit polo�ku ze seznamu argument�
func_get_args -- Vr�tit pole obsahuj�c� seznam argument� funkce
func_num_args -- Vr�tit po�et argument� p�edan�ch funkci
function_exists -- Vr�tit TRUE, pokud je dan� funkce definov�na
get_defined_functions --  Returns an array of all defined functions
register_shutdown_function -- Zaregistrovat funkci pro proveden� p�i ukon�en� b�hu
register_tick_function --  Register a function for execution on each tick
unregister_tick_function --  De-register a function for execution on each tick


add a note add a note User Contributed Notes
Funkce pro pr�ci s funkcemi
php-note-2003-june-18 at ryandesign dot com
18-Jun-2003 03:22
Xavier's example is rather complicated, and his task would be much more simply accomplished by using classes. Define a base class to do the basic functions open, dump, and close, and create extension classes to override whatever behavior.

class foo {
  function open() {
   // Default functionality for open()
  }
  function dump() {
   // Default functionality for dump()
  }
  function close() {
   // Default functionality for close()
  }
}

class bar extends foo {
  function open() {
   // Override functionality of open()
  }
  // dump() and close() remain as in class foo
}
xmontero at dsitelecom dot com
23-Aug-2002 09:49
You can do some kind of overloading functions in PHP using "function_exists".

Let's suppose you want a script to call plugins that are php files and you want the plugins to "override" behaviours that if not overriden are used by default.

You might for example write a "mySQL table viewer" which displays a table and it "opens a table", "dumps the header", "dumps row-by-row" and then "closes the table".

Let's suppose you want a plugin for "Stocks" which prints negative stocks in red, so you want to override the "dumping row-by-row" to do that behaviour. Instead you do not want to have all the "default behaviour" repeated in the plugin.

You may then do the following:

1) Define a default plugin (ex: "default.php"

2) Write all your functions in default.php to open, dump and close, but add them a suffix:

open_table_default()
dump_header_default()
dump_row_default()
close_table_default()

3) Call your functions with a wrapper: Insetad of this:
open_table() or open_table_default() write this:

plugincall("open_table");

4) Then write a function called plugincall which uses function_exists() to see if you call one or the other function:

function plugincall($desired_function_name)
{

if( function_exists( $desired_function_name))
{
   //Call the plugin
   //Note the $ before the name
   $desired_function_name()
}
else
{
  $desired_function_name = $desired_function_name . "_default";
  if( function_exists($desired_function_name))
  {
     //Call the default
     //Note the $ before the name
     $desired_function_name()
  }
  else
  {
     // Nor the plugin nor the default were defined
  }
}

So, now, if the require_once(plugin.php) contains a function called "open_table()", it will be used. Instaed, "open_table_default()" will be used.

It's not like overloading functions, but it is very useful to write scripts ampliable by other programmers: If they write a function, it is used, if not, the default is used.

See ya!
Xavier Montero.

<ftp_systypecall_user_func_array>
 Last updated: Thu, 15 Jul 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This unofficial mirror is operated at: /
Last updated: Sun Nov 14 23:09:54 2004 Local time zone must be set--see zic manual page