duanjia4097 2014-11-17 05:04
浏览 11
已采纳

在调用PHP方法之前设置$ _POST数据

Is it possible to set $_POST data before calling another PHP method?

I'm trying to call a method from another PHP class. That method do not accept any arguments, it only takes $_POST data. So, my question is: is it possible to do something like this?

$_POST['name'] = 'John';

$user = new User();
$user->create();

The method "create" from the class "User" gets the $_POST['name'] data to create the record. Does it work?

  • 写回答

1条回答 默认 最新

  • dso15221 2014-11-17 05:35
    关注

    Yes, you can do like this.

    Since $_POST is a superglobal associative array, you can change the values like a normal php array. Take a look at this piece of code. It will print "Hai".

    <?php
     class first{
       public function index(){
         $_POST['text'] = 'Hai';
         $scnd = new second();
         $scnd->disp();
       }
     }
     class second{
       public function disp(){
         $cc = $_POST['text'];
         echo $cc;
       }
     }
     $in = new first();
     $in->index();
     ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部