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

PHP function categories in classes / Cases in parameters

我要修改这段代码:

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);

(这个函数实际上是通过读取$mode来理解它要做什么)

下面的代码。 此代码无效。我想告诉我我必须做哪些改变才能让它工作

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);

我知道第二个代码很乱。它不起作用,但我想做这样的事情。对功能和参数进行分类。我希望你明白了。也许没有这样的方法,我必须在第一个代码中使用 $mode 方法?

您正在尝试分解函数并从各个部分创建一个 class。

您的代码中存在一些语法错误。 您在这里混淆了很多概念:函数、classes、静态和动态调用。请阅读PHP手册中关于OOP的基本章节:http://php.net/manual/en/language.oop5.php


这部分是错误的。

class send($message)
{

class 定义以关键字 class 开头,后跟 class 名称:

class Send {

}

您不能直接在 class 中使用它,您可以将它包装在构造函数或函数中。

$msg = ">> " . $message;

您可以声明一个接受参数的构造函数:

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);
    }
}

此 class 在构造函数中接受消息并将其设置为 属性。 然后您可以在函数中重用它($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');

我弄明白了,非常感谢 Jens A. Koch

致所有在场的人:

在 PHP5.4 之前:

class send {
    public function __construct($msg) {
        $this -> message = $msg;
    }
    public function clients(/* many params */) {
        echo "some clients say: ".$this->message;
    }
    public function client(/* many params */) {
        echo "one client says: ".$this->message;
    }
    public function all(/* many params */) {
        echo "all clients say: ".$this->message;
    }
    // function b (build) returns the class
    public function b($msg) {return new self($msg);}
}

// CALLING IT
send::b("test")->clients(); // >> some clients say: test

PHP 5.4+(不幸的是我有 5.3 但它应该工作) "public function b" 不需要:

class send {
    public function __construct($msg) {
        $this -> message = $msg;
    }
    public function clients(/* many params */) {
        echo "some clients say: ".$this->message;
    }
    public function client(/* many params */) {
        echo "one client says: ".$this->message;
    }
    public function all(/* many params */) {
        echo "all clients say: ".$this->message;
    }
}
// CALLING IT
(new send("test"))->clients();