dsdqpdjpq16640651 2015-04-18 21:06
浏览 50
已采纳

类中的PHP函数类/参数中的案例

I want to modify this code:

function send($message, $mode, $param1, $param2)
{
    $msg = ">> " . $message;

    if ($mode == "client") {
        $client = $param1; // $param1 is a websocket source variable
        // code
    }
    elseif ($mode == "clients") {
        $clients = $param1; // now $param1 is an array of websocket sources
        // code
    }
    elseif ($mode == "all") {
        // code
    }
}

send("Hello World!", "all", $whatever1, $whatever2);

(this function actually reads $mode to understand what it is going to do)

to the code below. This code WILL NOT work. I would you like to tell me what changes i have to do for it to work

class send($message)
{
    $msg = ">> " . $message;

    public function client($client, $param2) { // $client is $param1 of previous code
        // code using $msg
    }

    public function clients($clients, $param2) { // $clients is an array and the $param1 of previous code
        // code using $msg
    }

    public function all($param2) {
        // code using $msg
    }
}

send("Hello World!")::all($whatever2);

I know the second code is very messed up. It doesn't work, but I want to do something like this. To categorize functions and parameters. I hope you got the idea. Maybe there is no such way and I have to use the $mode method in the first code?

  • 写回答

2条回答 默认 最新

  • doubi1931 2015-04-18 21:55
    关注

    You are trying to decompose the function and create a class from the parts.

    There are some syntax errors in your code. You are mixing up a lot of concepts here: functions, classes, static and dynamic calls. Please read the basic chapters in the PHP manual about OOP: http://php.net/manual/en/language.oop5.php


    This part is wrong.

    class send($message)
    {
    

    A class definition begins with the keyword class, followed by a class name:

    class Send {
    
    }
    

    You can not use this directly inside a class, you could wrap it in the constructor or in a function.

    $msg = ">> " . $message;
    

    You can declare a constructor which accepts a parameter:

    public function __construct($message) {
        $this->message = $message;
    }
    

    class Send
    {        
        private $message = '';
    
        public function __construct($message) {
            $this->message = $message;
        }
    
        public function toClient($client, $param) {
            var_dump($this->message, $client, $param);
        }
    
        public function toClients($clients, $param) {
            var_dump($this->message, $clients, $param);
        }
    
        public function toAll($param) {
            var_dump($this->message, $param);
        }
    }
    

    This class accept the message in the constructor and sets it as a property. You might then reuse it across the functions ($this->message).


    // Usage
    
    $message = 'Hello World';
    
    // instantiate the class
    // and pass the message you want to send as param
    // execute a specific class method 
    
    $send = new Send($message);
    $send->toClient('john', 'more');
    
    // with PHP 5.4 it's a one-liner
    (new Send($message))->toClient('john', 'more');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection