0) {...}"; 3. Dynamically create methods through "create_function()" statements That’s it."/> 0) {...}"; 3. Dynamically create methods through "create_function()" statements That’s it.">

Home >Backend Development >PHP Problem >How to dynamically create methods in php

How to dynamically create methods in php

藏色散人
藏色散人Original
2021-09-19 09:22:551788browse

php dynamic creation method: 1. Create a PHP sample file; 2. Create conditional statements through "if (count($_POST) > 0) {...}"; 3. Through "create_function" ()" statement can dynamically create a method.

How to dynamically create methods in php

#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.

How to dynamically create methods in php?

How to dynamically create functions during PHP runtime

General language functions must be defined at runtime, and PHP supports dynamically creating functions at runtime, as follows It is a simple example that creates functions according to different conditions during exercise $a

<?php
if (count($_POST) > 0) {
  $prepped = create_function(&#39;$a&#39;, &#39;return trim($_POST[$a]);&#39;);
}
elseif (count($_GET) > 0) {
  $prepped = create_function(&#39;$a&#39;, &#39;return strtoupper($_GET[$a]);&#39;);
}
else {
  $prepped = create_function(&#39;$a&#39;, &#39;return false;&#39;);
}echo $prepped(&#39;file&#39;);
?>

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to dynamically create methods in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn