1

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?

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
marirena
  • 300
  • 2
  • 10

2 Answers2

0

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');
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
  • very very good. but because i don't want to call it with 2 lines, what can i do to run it in one line? like Send("message")::toClient('john', 'more') – marirena Apr 18 '15 at 22:32
  • Glad i could help. Please forget about the Scope Resolution Operator (::). It's easy to get trapped into a bad design when using statics. What you are looking for is called "method chaining" or "daisy chaining". See, for instance, here: http://stackoverflow.com/questions/12853573/how-to-daisy-chain-php-classes & http://stackoverflow.com/questions/2188629/how-to-chain-method-on-a-newly-created-object You simply return the object at the end of a method (`return $this;`). In other words: the methods of a class return the object itself and this allows the following call to another method. – Jens A. Koch Apr 18 '15 at 22:41
  • If you are using PHP 5.4+ then you can use a one-liner to instantiate the class and call a class method. Updated my answer (see last line). – Jens A. Koch Apr 18 '15 at 22:48
0

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();
marirena
  • 300
  • 2
  • 10