Summary of common usage of PHP functions

  • 2020-03-31 20:15:09
  • OfStack

Magic function

 

Magic functions are a language feature built into PHP. When a program executes to a certain point, if these magic functions are defined (the PHP manual calls them "Overloading"), PHP will call them and pass the corresponding parameters, which can be considered as hook functions in PHP execution. The common magic functions are * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * They can be used to automatically load containing files, implement deferred execution (similar to the property accessor in.net), garbage collection, object clone, etc., for example, with successive autoload, see Magic Method for other Magic functions.

 

S: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s: s:

 

Here is a simple example

The function __autoload ($class_name)  

{

        Require_once $class_name. '. Class. PHP ';

}

Put it into a global include file such as common.php. When creating a new object, such as $obj=new Class_A, if PHP cannot find Class_A, it will take "Class_A" as the argument $class_name, and then execute the function of 's. This will automatically include the header file.

 

Additional constants and functions:

S/s: magic constant, get the path of the current source file (with filename)

S/s: magic constant, gets the class name of the current class (case sensitive).

Array get_included_files (void) : built-in function that returns a list of files contained by include(), include_once(), require(), or require_once(), but not by the auto_prepend_file entry set in the php.ini configuration file. Also get_required_files() is just an alias for get_included_files (void).

String dirname (string path) : returns the directory part of the path.

 

Anonymous functions

 

Using anonymous functions before PHP5.3, you can create anonymous functions with create_function(),

$func = create_function (' $a, $b ', '

If ($a = = $b)

{

                Return 0;

}

  Return ($a< $b)? 1:1;

');

In PHP5.3, you can use the lamda expression directly

$func = function ($a, $b)

{

If ($a = = $b)

{

                Return 0;

}

  Return ($a< $b)? 1:1;

}

You can then pass in other functions as arguments, such as usort($arr,$func); It can also be called directly, $func(3,4);

 

Closures are used in PHP5.3

The function foo ($arg1)

{

$var = 3;

$inner = function ($innerArg) use ($arg1, $var)

{

    Return $innerArg + $arg1 / $var.

};

Echo $inner (5);

}

Closures are treated as a built-in class, but are not as flexible as javascript and cannot have properties. $inner - > A = 5; It's illegal.

 

Nested function

 

Nested functions can be defined in the parent function body, such as:

The function the outer ()

{

      $out_var = 1;

      The function inner ()

      {

              Var_dump ($out_var); // output NULL, can not access $out_var, declare global, $out_var is not a global variable

              Echo "call inner \ n";

      }

      / / echo "call outer \ n";

 

      Inner (); // if inner() is not called, then outer() is not called either

}

 

The outer ();

Inner (); // although the function is global, the outer() must be called first, otherwise the inner() call will report an error (the function is undefined)

 

Conditions for function

 

$debug = false;

If ($debug = = = true)

{

      The function foo ()

      {

              Echo "foo";

      }

}

 

Foo ();

 

From here we can guess why inner() is not called before outer() is called above. Because inner was not "compiled" before the call to outer().

 

 

Function dynamic call

 

Function names can be variable names.

The function foo ()

{

Echo "call foo";

}

$func = 'foo';

$func ();

This is a dangerous approach. It is recommended to use the whitelist method to write the available function names in the configuration file, or to have a uniform prefix for the function names, such as $func="act_". . Also, function_exists& providesare available before invocation; ($funcName) to check if the function exists.

 

Call_user_func (callback function, [, mixed parameter [, mixed...

In addition, you can get all defined functions(note all, including built-in functions) through get_defined_functions()

 

The method of calling the class is passed an array as an argument. The first element of the array is the instance or class name, and the second element is the method name:

The class myclass

{

      Public $age = 21;

 

      The function echo_age ()

      {

              Echo $this - > The age;

      }

      The static function s_echo_age ()

      {

              Echo 22;

      }

      The function i_echo_age ()

      {

              Echo 23;

      }

}

$c = new myclass;

$classname = "myclass";

// calls the instance method if call_user_func(array($classname, 'echo_age')); An error is reported and $this does not point to any instance

Call_user_func (array ($c, 'echo_age'));

Echo "\ n";

// calls the instance method, and if there is no reference to the $this variable, there is no problem

Call_user_func (array ($classname, 'i_echo_age'));

Echo "\ n";

// calls static methods, passing in either the class name or an instance

Call_user_func (array ($classname, 's_echo_age'));

Echo "\ n";

Call_user_func (array ($c, 's_echo_age'));

 

 

Get function parameters

 

In this way, you can achieve very flexible overloading, but it is easy to make the logic responsible and modest.

Func_get_arg (int arg_num) gets the first arg_num parameter (count from 0)

Func_num_args () gets the total number of parameters

Func_get_args () gets all the parameters.

 

Implement a design pattern in Gof with three lines of code:

The function call_it ($func)

{

$args = func_get_args ();

The array_shift ($args); // remove the first argument, which is the name of the function

Call_user_func_array -- $func, $args);

}

 

The function to the add ($a, $b)

{

      Echo $+ $b;

}

The function SQR ($a)

{

      Echo $a * $a;

}

Call_it (' add ', 1, 2);

Echo "\ n";

Call_it (' SQR, 2);


Related articles: