类中的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?

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

I figured it out, many many thanks to Jens A. Koch

For all of you out there:

Prior to PHP 5.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+ (unfortunately I have 5.3 but it should work) "public function b" is not needed:

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