基本链接方法是返回类错误的对象

时间:2016-12-22 21:35:14

标签: php

我正在尝试创建一个简单的链式getter / setter。所以我可以像这样访问它:

// initiate class
$testModel = new TestModel();

// setters
$testModel
    ->setUser("Hello")
    ->setEmail("Test@gmail.com");

// getters
$testModel
    ->getUser()
    ->getEmail();

当我v v时,我得到以下内容:

object(Filtration\Model\TestModel)#5 (2) { ["User"]=> string(5) "Hello" ["Email"]=> string(14) "Test@gmail.com" }

但当我回复getters时,我可以将它打印在屏幕上,我得到:     可捕获的致命错误:类的对象Filtration \ Model \ TestModel无法转换为字符串

我的getter / setter类:

Class TestModel
{

    /**
     * [description here]
     *
     * @return [type] [description]
     */
    public function getUser() {
        return $this;
    }

    /**
     * [Description]
     *
     * @param [type] $newUser [description]
     */
    public function setUser($User) {
        $this->User = $User;

        return $this;
    }


    /**
     * [description here]
     *
     * @return [type] [description]
     */
    public function getUsername() {
        return $this;
    }

    /**
     * [Description]
     *
     * @param [type] $newUsername [description]
     */
    public function setUsername($Username) {
        $this->Username = $Username;

        return $this;
    }


    /**
     * [description here]
     *
     * @return [type] [description]
     */
    public function getEmail() {
        return $this;
    }

    /**
     * [Description]
     *
     * @param [type] $newEmail [description]
     */
    public function setEmail($Email) {
        $this->Email = $Email;

        return $this;
    }


    /**
     * [description here]
     *
     * @return [type] [description]
     */
    public function getFirstname() {
        return $this->Firstname;
    }

    /**
     * [Description]
     *
     * @param [type] $newFirstname [description]
     */
    public function setFirstname($Firstname) {
        $this->Firstname = $Firstname;

        return $this;
    }
}

3 个答案:

答案 0 :(得分:0)

由于您需要方法处理,因此必须在每个getter / setter中返回$this。因此

echo $testModel->setEmail('foo')->setName('bar');
基本上,

是回显一个物体。您可以在对象中定义__toString魔术方法,因此可以回显:

class TestModel
{
    protected $email;

    protected $name;

    // getters & setters

    // Should always return string
    public function __toString()
    {
        return 'Hey, I'm a string representation of the object! My email is '.$email' and name is '.$name;
    }
}

但是,如果你想通过getter链接来实现某种连接 - 这是不可能的。

答案 1 :(得分:0)

Getter不应该是可链接的,因为它们需要返回引用的值。塞特犬可以连锁。通过在getter中返回$this,您无法传递getter方法请求的实际值,这会使getter变得毫无意义。

我会更新这样的类:

Class TestModel
{
    private $User;
    private $Username;
    private $Email;
    private $Firstname;

    /**
     * [description here]
     *
     * @return [type] [description]
     */
    public function getUser() {
        return $this->User;
    }

    /**
     * [Description]
     *
     * @param [type] $newUser [description]
     */
    public function setUser($User) {
        $this->User = $User;

        return $this;
    }


    /**
     * [description here]
     *
     * @return [type] [description]
     */
    public function getUsername() {
        return $this->Username;
    }

    /**
     * [Description]
     *
     * @param [type] $newUsername [description]
     */
    public function setUsername($Username) {
        $this->Username = $Username;

        return $this;
    }


    /**
     * [description here]
     *
     * @return [type] [description]
     */
    public function getEmail() {
        return $this->Email;
    }

    /**
     * [Description]
     *
     * @param [type] $newEmail [description]
     */
    public function setEmail($Email) {
        $this->Email = $Email;

        return $this;
    }


    /**
     * [description here]
     *
     * @return [type] [description]
     */
    public function getFirstname() {
        return $this->Firstname;
    }

    /**
     * [Description]
     *
     * @param [type] $newFirstname [description]
     */
    public function setFirstname($Firstname) {
        $this->Firstname = $Firstname;

        return $this;
    }
}

然后使用它:

$testModel = new TestModel();

// setters
$testModel
    ->setUser("Hello")
    ->setEmail("Test@gmail.com");

// getters
echo $testModel->getUser();
echo $testModel->getEmail();

答案 2 :(得分:0)

你几乎拥有它。问题是,在调用某些getter方法时,您没有返回正确的信息。例如,getEmail()应该返回$this->email而不是$this

这就是我构建你的课程的方式:

Class UserModel
{
    private $name;
    private $email;

    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    public function setEmail($email)
    {
        $this->email = $email;
        return $this;
    }

    public function getName()
    {
        return $this->name;
    }

    public function getEmail()
    {
        return $this->email;
    }
}

这就是我如何使用它:

$user1 = new UserModel();

// setter
$user1
    ->setName("John")
    ->setEmail("john@gmail.com");

// Shows all the user's information
var_dump($user1);

// Shows specific details of that user (EMAIL)
var_dump($user1->getEmail());

// Shows specific details of that user (NAME)
var_dump($user1->getName());

另一种做法

不要因为单向行动而感到受限制。有多种方法可以达到相同的效果。这取决于您打算如何使用它。这是另一种选择:

Class UserModel
{
    private $name;
    private $email;

    public function __construct($args)
    {
        $this->name = $args["name"];
        $this->email = $args["email"];
    }

    public function getName()
    {
        return $this->name;
    }

    public function getEmail()
    {
        return $this->email;
    }
}

使用:

$data = [
    "name" => "John",
    "email" => "john@gmail.com",
];

// initiate class
$user1 = new UserModel($data);

// Shows all the user's information
var_dump($user1);

// Shows specific details of that user (EMAIL)
var_dump($user1->getEmail());

// Shows specific details of that user (NAME)
var_dump($user1->getName());