Php 模拟JavaScript扩展函数

Php 模拟JavaScript扩展函数,php,Php,我想知道在PHP中是否可以像JavaScript一样,将函数调用应用于对象的返回。在JavaScript中,我可以做到这一点: var y = 1; var x = y.toString().concat(' + 1'); console.log(x); 我想如果在PHP中也能做到这一点的话。我在递归地思考这个问题,我不知道搜索它的确切名称。我现在正在努力: <?php class Main { public function __construct() {

我想知道在PHP中是否可以像JavaScript一样,将函数调用应用于对象的返回。在JavaScript中,我可以做到这一点:

var y = 1;
var x = y.toString().concat(' + 1');
console.log(x);
我想如果在PHP中也能做到这一点的话。我在递归地思考这个问题,我不知道搜索它的确切名称。我现在正在努力:

<?php
    class Main {
        public function __construct() {
            $this->Main = new Main;
        }

        public function merge(/* this */ $xs, $ys) {
            return array_merge($xs, $ys);
        }

        public function add(/* this */ $xs, $ys) {
            return array_push($xs, $ys);
        }
    }

    $aux = new Main;
    $x = $aux   -> merge([1, 2, 3], [4, 5, 6])
                -> add(7)
                -> add(8)
                -> add(9);
    // $x => [1, 2, 3, 4, 5, 6, 7, 8, 9]
?>

我能像JavaScript那样做到这一点吗?与C#扩展方法几乎相同。

您可以使用函数返回,但它没有JavaScript好看:

<?php

$array1 = array(1, 2, 3);
$array2 = array(4, 5, 6);

$x = array_push(array_push(array_push(array_merge($array1, $array2), 7), 8), 9);

您可以使用函数的返回,但它没有JavaScript好看:

<?php

$array1 = array(1, 2, 3);
$array2 = array(4, 5, 6);

$x = array_push(array_push(array_push(array_merge($array1, $array2), 7), 8), 9);

您可以使用函数的返回,但它没有JavaScript好看:

<?php

$array1 = array(1, 2, 3);
$array2 = array(4, 5, 6);

$x = array_push(array_push(array_push(array_merge($array1, $array2), 7), 8), 9);

您可以使用函数的返回,但它没有JavaScript好看:

<?php

$array1 = array(1, 2, 3);
$array2 = array(4, 5, 6);

$x = array_push(array_push(array_push(array_merge($array1, $array2), 7), 8), 9);

其被称为方法链接:

class Main {
    private $ar = array();

    public function merge($xs, $ys) {
        $this->ar = array_merge($xs, $ys);
        return $this;
    }

    public function add($ys) {
        $this->ar[]= $ys;
        return $this;
    }


    public function toArray(){
        return $this->ar;
    }

    //if you want to echo a string representation
    public function __toString(){
        return implode(',', $this-ar);
    }
}
$aux = new Main;
$x = $aux->merge([1, 2, 3], [4, 5, 6])->add(7)->add(8)-> add(9);
echo $x;
var_dump($x->toArray());

其被称为方法链接:

class Main {
    private $ar = array();

    public function merge($xs, $ys) {
        $this->ar = array_merge($xs, $ys);
        return $this;
    }

    public function add($ys) {
        $this->ar[]= $ys;
        return $this;
    }


    public function toArray(){
        return $this->ar;
    }

    //if you want to echo a string representation
    public function __toString(){
        return implode(',', $this-ar);
    }
}
$aux = new Main;
$x = $aux->merge([1, 2, 3], [4, 5, 6])->add(7)->add(8)-> add(9);
echo $x;
var_dump($x->toArray());

其被称为方法链接:

class Main {
    private $ar = array();

    public function merge($xs, $ys) {
        $this->ar = array_merge($xs, $ys);
        return $this;
    }

    public function add($ys) {
        $this->ar[]= $ys;
        return $this;
    }


    public function toArray(){
        return $this->ar;
    }

    //if you want to echo a string representation
    public function __toString(){
        return implode(',', $this-ar);
    }
}
$aux = new Main;
$x = $aux->merge([1, 2, 3], [4, 5, 6])->add(7)->add(8)-> add(9);
echo $x;
var_dump($x->toArray());

其被称为方法链接:

class Main {
    private $ar = array();

    public function merge($xs, $ys) {
        $this->ar = array_merge($xs, $ys);
        return $this;
    }

    public function add($ys) {
        $this->ar[]= $ys;
        return $this;
    }


    public function toArray(){
        return $this->ar;
    }

    //if you want to echo a string representation
    public function __toString(){
        return implode(',', $this-ar);
    }
}
$aux = new Main;
$x = $aux->merge([1, 2, 3], [4, 5, 6])->add(7)->add(8)-> add(9);
echo $x;
var_dump($x->toArray());