4zhvml8 发表于 2024-10-5 09:18:04

PHP 运用变量函数声明和应用回调函数


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>变量函数声明和应用回调函数</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>要自定义一个<span style="color: black;">能够</span>回调的函数, <span style="color: black;">能够</span><span style="color: black;">选取</span><span style="color: black;">运用</span>变量函数<span style="color: black;">帮忙</span>实现。</p><span style="color: black;"><span style="color: black;">&lt;?php</span>
      <span style="color: black;">/** 声明回调函数filter, 在0-100的整数中<span style="color: black;">经过</span>自定义<span style="color: black;">要求</span>过滤不要的数字
            <span style="color: black;">@param</span> callback $fun 需要传递一个函数名<span style="color: black;">叫作</span>字符串<span style="color: black;">做为</span>参数
            */</span>
      <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">filter</span><span style="color: black;">( $fun )</span> </span>{
      <span style="color: black;">for</span>($i=<span style="color: black;">0</span>; $i &lt;= <span style="color: black;">100</span>; $i++) {
      <span style="color: black;">//将参数变量$fun加上一个圆插号$fun(), 则为调用和变量$fun值同名的函数</span>
      <span style="color: black;">if</span>( $fun($i) )
      <span style="color: black;">continue</span>;

      <span style="color: black;">echo</span> $i.<span style="color: black;">&lt;br&gt;</span>;
      }
      }

      <span style="color: black;">/** 声明一个函数one, <span style="color: black;">倘若</span>参数是3的倍数就返回true, 否则返回false<span style="color: black;">@param</span> int $num 需要一个整数<span style="color: black;">做为</span>参数
            */</span>
      <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">one</span><span style="color: black;">($num)</span> </span>{
      <span style="color: black;">return</span> $num%<span style="color: black;">3</span> == <span style="color: black;">0</span>;
      }

      <span style="color: black;">/** 声明一个函数two, <span style="color: black;">倘若</span>参数是一个回文数(翻转后还等于自己的数)返回true, 否则返回false<span style="color: black;">@param</span> int $num 需要一个整数<span style="color: black;">做为</span>参数
            */</span>
      <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">two</span><span style="color: black;">($num)</span> </span>{
      <span style="color: black;">return</span> $num == strrev($num);
      }

      filter(<span style="color: black;">"one"</span>); <span style="color: black;">//打印出100以内非3的倍数,参数"one"是函数one()的名<span style="color: black;">叫作</span>字符串,是一个回调</span>
      <span style="color: black;">echo</span> <span style="color: black;">--------------------&lt;br&gt;</span>;
      filter(<span style="color: black;">two</span>); <span style="color: black;">//打印出100以内非回文数,参数"two"是函数two()的名<span style="color: black;">叫作</span>字符串,是一个回调</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">借助call_user_func_array()函数自定义回调函数</p><span style="color: black;"><span style="color: black;">&lt;?php</span>
      <span style="color: black;">/** 声明一个函数fun(), 功能只输出两个字符串,目的是<span style="color: black;">做为</span>call_user_func_array()回调参数
            <span style="color: black;">@param</span> string $msg1 需要传递一个字符串<span style="color: black;">做为</span>参数
            <span style="color: black;">@param</span>string $msg2 需要传递另一个字符串<span style="color: black;">做为</span>参数
            */</span>
      <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">fun</span><span style="color: black;">($msg1, $msg2)</span> </span>{
      <span style="color: black;">echo</span> <span style="color: black;">$msg1 = </span>.$msg1;
      <span style="color: black;">echo</span> <span style="color: black;">&lt;br&gt;</span>;
      <span style="color: black;">echo</span> <span style="color: black;">$msg2 = </span>.$msg2;
      }

      <span style="color: black;">/** <span style="color: black;">经过</span>系统函数call_user_func_array() 调用函数fun()
            <span style="color: black;">第1</span>个参数为函数fun()的名<span style="color: black;">叫作</span>字符串
            第二个参数则是一个数组,<span style="color: black;">每一个</span>元素值会按<span style="color: black;">次序</span>传递给调用的fun()函数参数列表中
            */</span>
      call_user_func_array(<span style="color: black;">fun</span>, <span style="color: black;">array</span>(<span style="color: black;">LAMP</span>, <span style="color: black;">张三</span>));
    </span><span style="color: black;"><span style="color: black;">&lt;?php</span>
      <span style="color: black;">/** 声明回调函数filter, 在0-100的整数中<span style="color: black;">经过</span>自定义<span style="color: black;">要求</span>过滤不要的数字<span style="color: black;">@param</span> callback $fun 需要传递一个函数名<span style="color: black;">叫作</span>字符串<span style="color: black;">做为</span>参数
            */</span>
      <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">filter</span><span style="color: black;">( $fun )</span> </span>{
      <span style="color: black;">for</span>($i=<span style="color: black;">0</span>; $i &lt;= <span style="color: black;">100</span>; $i++) {
      <span style="color: black;">//<span style="color: black;">运用</span>系统函数call_user_func_array(),调用变量$fun值相同的函数</span>
      <span style="color: black;">if</span>( call_user_func_array($fun, <span style="color: black;">array</span>($i)) )
      <span style="color: black;">continue</span>;

      <span style="color: black;">echo</span> $i.<span style="color: black;">&lt;br&gt;</span>;
      }
      }

      <span style="color: black;">/** 声明一个函数one, <span style="color: black;">倘若</span>参数是3的倍数就返回true, 否则返回false
            <span style="color: black;">@param</span>int $num 需要一个整数<span style="color: black;">做为</span>参数
            */</span>
      <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">one</span><span style="color: black;">($num)</span> </span>{
      <span style="color: black;">return</span> $num%<span style="color: black;">3</span> == <span style="color: black;">0</span>;
      }

      <span style="color: black;">/** 声明一个函数two, <span style="color: black;">倘若</span>参数是一个回文数(翻转后还等于自己的数)返回true, 否则返回false
            <span style="color: black;">@param</span>int $num 需要一个整数<span style="color: black;">做为</span>参数
            */</span>
      <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">two</span><span style="color: black;">($num)</span> </span>{
      <span style="color: black;">return</span> $num == strrev($num);
      }
      filter(<span style="color: black;">"one"</span>); <span style="color: black;">//打印出100以内非3的倍数,参数"one"是函数one()的名<span style="color: black;">叫作</span>字符串,是一个回调</span>
      <span style="color: black;">echo</span> <span style="color: black;">--------------------&lt;br&gt;</span>;
      filter(<span style="color: black;">two</span>); <span style="color: black;">//打印出100以内非回文数,参数"two"是函数two()的名<span style="color: black;">叫作</span>字符串,是一个回调</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">类静态函数和对象的<span style="color: black;">办法</span>回调</p><span style="color: black;"><span style="color: black;">&lt;?php</span>
      <span style="color: black;">/* 声明一个类Demo,类中声明一个静态的成员<span style="color: black;">办法</span>fun() */</span>
      <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Demo</span> </span>{
      <span style="color: black;">static</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">fun</span><span style="color: black;">($msg1, $msg2)</span> </span>{
      <span style="color: black;">echo</span> <span style="color: black;">$msg1 = </span>.$msg1;
      <span style="color: black;">echo</span> <span style="color: black;">&lt;br&gt;</span>;
      <span style="color: black;">echo</span> <span style="color: black;">$msg2 = </span>.$msg2;
      }
      }

      <span style="color: black;">/* 声明一个类Test, 类中声明一个普通的成员<span style="color: black;">办法</span>fun() */</span>
      <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Test</span> </span>{
      <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">fun</span><span style="color: black;">($msg1, $msg2)</span> </span>{
      <span style="color: black;">echo</span> <span style="color: black;">$msg1 = </span>.$msg1;
      <span style="color: black;">echo</span> <span style="color: black;">&lt;br&gt;</span>;
      <span style="color: black;">echo</span> <span style="color: black;">$msg2 = </span>.$msg2;
      }
      }

      <span style="color: black;">/** <span style="color: black;">经过</span>系统函数call_user_func_array()调用Demo类中的静态成员<span style="color: black;">办法</span>fun(),
            回调类中的成员<span style="color: black;">办法</span>:第一个参数必须<span style="color: black;">运用</span>数组,并且这个数组需要指定两个元素,
            <span style="color: black;">第1</span>个元素为类名<span style="color: black;">叫作</span>字符串,第二个元素则是该类中的静态<span style="color: black;">办法</span>名<span style="color: black;">叫作</span>字符串。
            第二个参数<span style="color: black;">亦</span>是一个数组,这个数组中<span style="color: black;">每一个</span>元素值会按<span style="color: black;">次序</span>传递给调用Demo类中的fun()<span style="color: black;">办法</span>参数列表中。
            */</span>
      call_user_func_array( <span style="color: black;">array</span>(<span style="color: black;">"Demo"</span>, <span style="color: black;">fun</span>), <span style="color: black;">array</span>(<span style="color: black;">LAMP</span>, <span style="color: black;">兄弟连</span>) );

      <span style="color: black;">/** <span style="color: black;">经过</span>系统函数call_user_func_array()调用Test类的实例对象中的成员<span style="color: black;">办法</span>fun(),
            回调类中的成员<span style="color: black;">办法</span>:<span style="color: black;">第1</span>个参数必须<span style="color: black;">运用</span>数组,并且这个数组需要指定两个元素,
            <span style="color: black;">第1</span>个元素为对象引用,在本例<span style="color: black;">亦</span><span style="color: black;">能够</span>是$obj=new Test()中的$obj, 第二个元素则是该对象中的成员<span style="color: black;">办法</span>名<span style="color: black;">叫作</span>字符串。
            第二个参数<span style="color: black;">亦</span>是一个数组,这个数组中<span style="color: black;">每一个</span>元素值会按<span style="color: black;">次序</span>传递给调用new Test()对象中的fun()<span style="color: black;">办法</span>参数列表中。
            */</span>
      call_user_func_array( <span style="color: black;">array</span>(<span style="color: black;">new</span> Test(), <span style="color: black;">fun</span>), <span style="color: black;">array</span>(<span style="color: black;">BroPHP</span>, <span style="color: black;">学习型PHP框架</span>) );
    </span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">call_user_func()<span style="color: black;">办法</span><span style="color: black;">运用</span></p><span style="color: black;"><span style="color: black;">&lt;?php</span>
      <span style="color: black;">//先引用后<span style="color: black;">增多</span></span>
      <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">_call</span><span style="color: black;">($call)</span></span>{
      <span style="color: black;">//<span style="color: black;">经过</span>传参获取call_user_func传过来的参数</span>
      <span style="color: black;">echo</span> $call++,<span style="color: black;">&lt;br/&gt;</span>;
      <span style="color: black;">echo</span> $call++,<span style="color: black;">"&lt;br/&gt;"</span>;
      }
      <span style="color: black;">//上面回调函数<span style="color: black;">无</span>返回值,<span style="color: black;">因此</span>,<span style="color: black;">这儿</span>就<span style="color: black;">无</span>返回值,_call为上面的函数的名<span style="color: black;">叫作</span></span>
      $re = call_user_func(<span style="color: black;">_call</span>,<span style="color: black;">1</span>);
      <span style="color: black;">//实验结果为 null,符合上面的结论</span>
      var_dump($re);
      <span style="color: black;">?&gt;</span></span><span style="color: black;"><span style="color: black;">&lt;?php</span>
      <span style="color: black;">//先<span style="color: black;">增多</span>后引用</span>
      call_user_func(<span style="color: black;"><span style="color: black;">function</span><span style="color: black;">($call)</span></span>{
      <span style="color: black;">echo</span> ++$call,<span style="color: black;">&lt;br/&gt;</span>;
      <span style="color: black;">echo</span> ++$call,<span style="color: black;">&lt;br/&gt;</span>;
      },<span style="color: black;">1</span>);<span style="color: black;">//传给匿名函数的参数为···1···,执行的结果为2,3</span>
      <span style="color: black;">?&gt;</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">参考:</p><span style="color: black;"><span style="color: black;">&lt;?php</span>
      <span style="color: black;">namespace</span> <span style="color: black;">app</span>\<span style="color: black;">portal</span>\<span style="color: black;">adapter</span>;
      <span style="color: black;">abstract</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">AbstractAdapter</span>
      </span>{
      <span style="color: black;">//要调用的<span style="color: black;">办法</span></span>
      <span style="color: black;">protected</span> $invokes;
      <span style="color: black;">/**
            * 调用invokes里的类和<span style="color: black;">办法</span>
            * <span style="color: black;">@param</span>$method
            *<span style="color: black;">@param</span> $params
            * <span style="color: black;">@param</span> $break
            * <span style="color: black;">@return</span> array
            */</span>
      <span style="color: black;">protected</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">callInvokes</span><span style="color: black;">($method, $params = [], $break = true)</span></span>{
      $params = (<span style="color: black;">array</span>)$params;
      $data = [];
      <span style="color: black;">foreach</span> (<span style="color: black;">$this</span>-&gt;invokes <span style="color: black;">as</span> $className) {
      <span style="color: black;">if</span>(class_exists($className)) {
      $class =<span style="color: black;">new</span> $className();
      <span style="color: black;">if</span> (method_exists($class, $method)) {
      $result = call_user_func_array([$class, $method], $params);
      <span style="color: black;">if</span>($result) {
      $data = array_merge($data, $result);<span style="color: black;">if</span> ($break === <span style="color: black;">true</span> || $break === $className) {
      <span style="color: black;">break</span>;
      }
      }
      }
      }
      }
      <span style="color: black;">return</span>$data;
      }
      }</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span><span style="color: black;">办法</span></p><span style="color: black;"><span style="color: black;">&lt;?php</span>
      <span style="color: black;">namespace</span> <span style="color: black;">app</span>\<span style="color: black;">portal</span>\<span style="color: black;">adapter</span>\<span style="color: black;">marketspider</span>;
      <span style="color: black;">use</span> <span style="color: black;">app</span>\<span style="color: black;">portal</span>\<span style="color: black;">adapter</span>\<span style="color: black;">AbstractAdapter</span>;
      <span style="color: black;">use</span> <span style="color: black;">think</span>\<span style="color: black;">Db</span>;
      <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Spider</span> <span style="color: black;">extends</span> <span style="color: black;">AbstractAdapter</span>
      </span>{
      <span style="color: black;">protected</span> $invokes = [
      BishijieSpider::class, <span style="color: black;">//获取app\portal\adapter\marketspider\BishijieSpider类</span>
      GateioSpider::class,
      ];
      <span style="color: black;">protected</span>$marketCoinData;<span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">getMarketCoin</span><span style="color: black;">()</span></span>{
      <span style="color: black;">if</span> (! <span style="color: black;">$this</span>-&gt;marketCoinData) {
      <span style="color: black;">$this</span>-&gt;marketCoinData = <span style="color: black;">$this</span>-&gt;callInvokes(<span style="color: black;">getMarketCoin</span>, [], <span style="color: black;">true</span>);
      <span style="color: black;">$this</span>-&gt;marketCoinData = array_column(<span style="color: black;">$this</span>-&gt;marketCoinData, <span style="color: black;">null</span>, <span style="color: black;">code</span>);
      }
      <span style="color: black;">return</span> <span style="color: black;">$this</span>-&gt;marketCoinData;
      }
      <span style="color: black;">/**
            * 更新币种到数据库
            * <span style="color: black;">@param</span>bool $update
            *<span style="color: black;">@return</span> array
            * <span style="color: black;">@throws</span> \think\Exception
            */</span>
      <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">setMarketCoinToDatabase</span><span style="color: black;">($update = false)</span></span>{
      $data =<span style="color: black;">$this</span>-&gt;getMarketCoin();
      $query = Db::name(<span style="color: black;">coin</span>);
      $insertData = [];
      <span style="color: black;">foreach</span> ($data <span style="color: black;">as</span> $v) {
      $code = strtoupper($v[<span style="color: black;">code</span>]);
      $where = [<span style="color: black;">code</span> =&gt; $code];
      $count = $query-&gt;where($where)-&gt;count();
      $arr = array_filter([
      <span style="color: black;">code</span> =&gt; $code,
      <span style="color: black;">name</span> =&gt; getVar($v, <span style="color: black;">name</span>),
      <span style="color: black;">name_ch</span> =&gt; getVar($v, <span style="color: black;">name_ch</span>),
      <span style="color: black;">name_en</span> =&gt; getVar($v, <span style="color: black;">name_en</span>),
      <span style="color: black;">source_code</span> =&gt; getVar($v, <span style="color: black;">source_code</span>),
      <span style="color: black;">source</span> =&gt; getVar($v, <span style="color: black;">source</span>),
      <span style="color: black;">total_value</span> =&gt; getVar($v, <span style="color: black;">total_value</span>),
      <span style="color: black;">addtime</span> =&gt; time(),
      ]);
      <span style="color: black;">if</span> (! $count) {
      $insertData[] = $arr;
      } <span style="color: black;">else</span> <span style="color: black;">if</span>($update) {
      $query-&gt;where($where)-&gt;update($arr);
      }
      }
      $insertData &amp;&amp; $query-&gt;insertAll($insertData);<span style="color: black;">return</span> $data;
      }
      <span style="color: black;">/**
            * 更新行情信息到数据库
            * <span style="color: black;">@return</span>array
            *<span style="color: black;">@throws</span> \think\Exception
            */</span>
      <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">setMarketInfoToDatabase</span><span style="color: black;">()</span></span>{
      $data = <span style="color: black;">$this</span>-&gt;getMarketCoin();
      $query = Db::name(<span style="color: black;">market_info</span>);
      $insertData = [];
      <span style="color: black;">foreach</span> ($data <span style="color: black;">as</span> $v) {
      $code = strtoupper($v[<span style="color: black;">code</span>]);
      $where = [<span style="color: black;">coin</span>=&gt; $code];
      $count = $query-&gt;where($where)-&gt;count();
      $arr = array_filter([<span style="color: black;">coin</span> =&gt; $code,
      <span style="color: black;">market</span> =&gt; strtolower($code) .<span style="color: black;">_</span>. <span style="color: black;">usdt</span>,
      <span style="color: black;">last_price</span> =&gt; getVar($v, <span style="color: black;">price</span>),
      <span style="color: black;">rate</span>=&gt; getRmbRate(),<span style="color: black;">percentchange</span> =&gt; getVar($v, <span style="color: black;">percentchange</span>),
      <span style="color: black;">basevolume</span> =&gt; getVar($v, <span style="color: black;">basevolume</span>),
      <span style="color: black;">addtime</span> =&gt; time(),
      ]);
      <span style="color: black;">if</span>(! $count) {
      $insertData[] = $arr;
      }<span style="color: black;">else</span> {
      $query-&gt;where($where)-&gt;update($arr);
      }
      }
      $insertData &amp;&amp; $query-&gt;insertAll($insertData);
      <span style="color: black;">return</span> $data;
      }
      <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">updateAll</span><span style="color: black;">($update = false)</span></span>{
      <span style="color: black;">$this</span>-&gt;setMarketCoinToDatabase($update);
      <span style="color: black;">$this</span>-&gt;setMarketInfoToDatabase();
      }
      }</span>




qzmjef 发表于 4 小时前

论坛的成功是建立在我们诚恳、务实、高效、创新和团结合作基础上,我们要把这种精神传递下去。
页: [1]
查看完整版本: PHP 运用变量函数声明和应用回调函数