How to dynamically create methods in php

藏色散人
Release: 2023-03-12 18:24:02
Original
1748 people have browsed it

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;);
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template