Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中是否可以将静态方法与非静态方法链接在一起?_Php_Static_Method Chaining - Fatal编程技术网

在PHP中是否可以将静态方法与非静态方法链接在一起?

在PHP中是否可以将静态方法与非静态方法链接在一起?,php,static,method-chaining,Php,Static,Method Chaining,这是我的示例代码类用户,但在我将静态方法添加到公共方法时,该用户不工作: <?php namespace App\Classic; class User { public $username; public static $upassword; public $age; public $message; public function username($username) { $this->username =

这是我的示例代码类用户,但在我将静态方法添加到公共方法时,该用户不工作:

<?php

namespace App\Classic;

class User
{
    public $username;
    public static $upassword;
    public $age;
    public $message;

    public function username($username)
    {
        $this->username = $username;
        echo $this->username."<br>";
        return $this;
    }

    public static function password($upassword)
    {
        self::$upassword = $upassword;
        echo self::$upassword."<br>";
    }

    public function age($age)
    {
        $this->age = $age;
        echo $this->age."<br>";
        return $this;
    }

    public function message($message)
    {
        $this->message = $message;
        echo $this->message."<br>";
        return $this;
    }
}

我不知道这样做背后的逻辑是什么,但这个解决方案还是有帮助的


我不知道这样做背后的逻辑是什么,但这个解决方案还是有帮助的


或者,您可以停止使用静态方法和变量。因为它们使您的代码在本质上是程序化的,并导致与特定类名的紧密耦合。静态属性和方法没有任何存在的理由。静态属性对于类的所有实例都是公共的。你想对所有用户使用相同的密码吗?哦,好的。谢谢,您可以停止使用静态方法和变量。因为它们使您的代码在本质上是程序化的,并导致与特定类名的紧密耦合。静态属性和方法没有任何存在的理由。静态属性对于类的所有实例都是公共的。你想对所有用户使用相同的密码吗?哦,好的。谢谢
$user = new User();
$user::password('secret')
     ->username('admin')
     ->age(40)
     ->message('lorem ipsum');
<?php

namespace App\Classic;

ini_set('display_errors', 1);

class User
{

    public $username;
    public static $upassword;
    public static $currentObject=null;//added this variable which hold current class object
    public $age;
    public $message;

    public function __construct()//added a constructor which set's current class object in a static variable
    {
        self::$currentObject= $this;
    }
    public function username($username)
    {
        $this->username = $username;
        echo $this->username . "<br>";
        return $this;//added this statment which will return current class object
    }

    public static function password($upassword)
    {
        self::$upassword = $upassword;
        echo self::$upassword . "<br>";
        return self::$currentObject;
    }

    public function age($age)
    {
        $this->age = $age;
        echo $this->age . "<br>";
        return $this;
    }

    public function message($message)
    {
        $this->message = $message;
        echo $this->message . "<br>";
        return $this;
    }

}

$user = new User();
$user::password('secret')
        ->username('admin')
        ->age(40)
        ->message('lorem ipsum');