A learning note on dynamic features in PHP. if you are not familiar with the PHP features, refer to this article. This article describes the dynamic characteristics of PHP in detail, let's take a look at the example. if you need it, please refer to it. this article is mainly summarized... A learning note on dynamic features in PHP. if you are not familiar with the PHP features, refer to this article. This article describes the dynamic characteristics of PHP in detail, let's take a look at the example. if you need it, please refer to it.
This article is mainly summarized for beginners who do not know the PHP features. Therefore, the demo code occupies a lot of space. let's see what else is missing to supplement it, you are welcome to email or correct the error in PHPChina's PHPer magazine.
Dynamic Language is a language that can change the program structure and variable type at runtime. for example, new classes and objects can be loaded and created, and new functions or methods can be added and removed, such as Smalltalk, Ruby, Python, PHP, Lua, Perl, and Groovy. Otherwise, it is a static language, such as C/C ++, Java, and C.
The dynamic characteristics of a dynamic language determine that it requires less code during development and has higher flexibility. the dynamic characteristics of PHP have laid the value of its existence, being familiar with the dynamic features of PHP makes it easier for us to use PHP.
1 weak type variable
Dynamic languages are designed to be weak types. that is to say, only after a variable is assigned a value can its data type be determined. when the code is executed, it will detect whether the variable is used illegally.
PHP variables are weak variables, which means that we do not need to declare the type of the variable. we can automatically detect the type of the variable at runtime and consider it as modifying the type of the variable.
2. PHP dynamic features
Feature 1. weak type variables
If the PHP variable is a weak variable, it means that we do not need to declare the type of the variable. at runtime, we can automatically detect the type of the variable and consider it as modifying the type of the variable. the code is as follows:
// PHP weak type variable example $ test = 1; print gettype ($ test ). ": $ testn"; $ test = 1.23456789; print gettype ($ test ). ": $ testn"; $ test = 'test'; // The variable automatically changes the type of print gettype ($ test ). ": $ testn"; $ test = array ('t' => 'Hi, I m from an array! '); Settype ($ test, 'object'); // changes the variable type print gettype ($ test ). ": {$ test-> t} n"; // test the variable Type if (is_object ($ Test) {print "test Type: My Type Is object. n ";}else {print gettype ($ test );}
The weak type does not mean that the code is insecure or not robust. extreme programming has already become a software development method. this method focuses on testing and uses a comprehensive unit testing solution to drive the development process, by executing the written code in different environments, you can ensure the security and robustness of the code. it is proved that in a weak language, combining weak types and unit tests is usually better than traditional system programming languages.
Feature 2. variable
You can dynamically set a variable name and use the following code:
// Variable example $ var = 'hi'; $ var = 'hello'; print $ var; print $ hi; // equivalent to the previous line
Feature 3. variable functions
This means that if a variable name has parentheses, PHP will look for a function with the same name as the value of the variable and will try to execute it. In addition to other things, this function can be used to implement the callback function, the code is as follows:
// Variable function example // function a () {print "I'm an";} // function B with a parameter ($ param) {print "I'm B, param: $ paramn" ;}$ x = 'a'; $ x (); $ x = 'B '; $ x ('xxxxxxxxxxxxxxx ');
Feature 4. variable length parameter list
When calling a function, the number of parameters of the function can be different. the code is as follows:
function foo() { $numargs = func_num_args(); print "Number of arguments: $numargsn"; print "the third argument: " . func_get_arg(2) . "n"; } foo(1, 2, '...@...'); foo(1, 2, '############', 4);
Feature 5: export variables from the array
Copy the data in the array and use the key name as the variable name and value as the variable value. The code is as follows:
// Example 1 $ a = array ('x' => 100, 'y' => 200, 'Z' => 300 ); // import the variable from the array to the current symbol table extract ($ a); // The function opposite to extract is compact () print "$ x $ y $ z "; // Example 2 $ a = array (100,200,300); list ($ x, $ z) = $ a; print "$ x $ z ";
Feature 6: use the value of an array as its key name and the value of another array as its value to create an array. the code is as follows:
$a = array('green', 'red', 'yellow'); $b = array('avocado', 'apple', 'banana'); $c = array_combine($a, $b); print_r($c);
Feature 7: dynamically create a function. the code is as follows:
// Lambda function $ newfunc = create_function ('$ a, $ B', 'Return "$ a + $ B = ". $ a + $ B; '); // creates an anonymous function print "nNew anonymous function: $ newfuncn"; print $ newfunc (2, 4); print "n "; // The effect is similar to function test ($ a, $ B) {return "$ a + $ B = ". $ a + $ B;} print test (2, 4 );
Feature 8. automatically load objects
You can define a _ autoload function, which is automatically called when trying to use a class that has not been defined. the code is as follows:
// ClassA. php. this code is written in ClassA. php class A {function _ construct () {print 'Yeah! ';}} 5 function _ autoload ($ className) {require_once $ className.'. php ';}
Feature 9, _ get, and _ set replace all accesses to the attribute variable array:
class Setter{ public $n; private $x = array("a" => 1, "b" => 2, "c" => 3); private function __get($nm) { echo "Getting [$nm]n"; if (isset($this->x[$nm])) { $r = $this->x[$nm]; print "Returning: $rn"; return $r; } else { echo "Nothing!n"; } } private function __set($nm, $val) { echo "Setting [$nm] to $valn"; if (isset($this->x[$nm])) { $this->x[$nm] = $val; echo "OK!n"; } else { echo "Not OK!n"; } } private function __isset($nm) { echo "Checking if $nm is setn"; return isset($this->x[$nm]); } private function __unset($nm) { echo "Unsetting $nmn"; unset($this->x[$nm]); } } $foo = new Setter(); $foo->n = 1; $foo->a = 100; $foo->a++; $foo->z++; var_dump(isset($foo->a)); //true unset($foo->a); var_dump(isset($foo->a)); //false // this doesn't pass through the __isset() method // because 'n' is a public property var_dump(isset($foo->n)); var_dump($foo);
Feature 10. Custom undefined methods
When you call an undefined method, the method name and parameters received by the method will be passed to the _ call method. the code is as follows:
class Caller { private $x = array(1, 2, 3); private function __call($m, $a) { print "Method $m called:n"; print_r($a); return $this->x; } } $foo = new Caller(); $a = $foo->test(1, "2", 3.4, true); print_r($a);
Feature 11. Custom errors and exception handling
After a custom error or exception handling function is set, when an error or exception occurs, the custom processing function is called to replace the system error handling function. the code is as follows:
Set_error_handler ('error _ handler'); // Set the UDF called when an error occurs. set the UDF to set_exception_handler () print $ a/0; // here the logic is wrong. when error_handler () is called, the error message "function error_handler ($ errno, $ message, $ filename, $ line) {if ($ errno & (E_ALL ^ E_NOTICE)" is returned )) {$ types = array (1 => 'error', 2 => 'warning', 4 => 'parse error', 8 => 'notice ', 16 => 'core error', 32 => 'core warning', 64 => 'compile error', 128 => 'compile warning' , 256 => 'User error', 512 => 'User warning', 1024 => 'User notic', 2048 => 'strict warning '); print "n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ N "; print $ types [$ errno]. ':'. $ message. 'In '. $ filename. 'On Line '. $ line. '. '; print "n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ N "; exit ;}}
Feature 12. high configurability, which can be changed:
PHP_INI_USER configuration options can be set in the user's PHP script or Windows Registry
You can set the PHP_INI_PERDIR configuration options in php. ini,. htaccess, or httpd. conf.
You can set the PHP_INI_SYSTEM configuration options in php. ini or httpd. conf.
The configuration options of PHP_INI_ALL can be set everywhere.
We should be familiar with php. you can use ini_set () function or a function that specifically modifies the runtime configuration to change the modifiable range to PHP_INI_ALL.
// Modify the configuration parameter ini_set ('session. save_path ', 'd:/temp') of php. // modify the configuration so that the script can save the session file to the D:/temp directory.
Feature 13. execute the php script in the code
Feature 14. php Working model (this feature is detailed in ).
As a pure explanatory language, PHP scripts initialize each time they are interpreted and terminate the execution after the interpretation. This operation is independent of each other. each request creates a separate process or thread to explain the corresponding page files, variables created on the page, and other objects, they are only visible inside the current page and cannot access the old computer.
After the termination, all external resources requested on the page that are not explicitly released by code, including memory, database connection, file handle, and Socket connection, will be forcibly released.
That is to say, PHP cannot directly access the variables that span pages at the language level, nor can it create objects that reside in the memory.
The advantage of PHP's unique working model is that it basically solves the headache of resource leakage. Web applications are characterized by a large number of concurrent processing in a short time, the application and release of various resources are very frequent, which may easily lead to leakage.
However, the disadvantage of this mechanism is also very obvious. the most direct consequence is that PHP cannot implement cross-page buffering at the language level.
Additionally, object cloning
When a PHP object is assigned a value to a variable, it is passed for reference. you need to clone the object to be copied.