【问题标题】:Setting the value of properties in PHP在 PHP 中设置属性的值
【发布时间】:2017-10-23 03:40:14
【问题描述】:

我有一个名为 members 的类,下面有一个示例。我要问的是如何设置标题的值。例如,我只允许 Mr、Mrs、Miss 和任何其他值将 throw out an error 声明 Only Mr,Mrs,Miss is allowed ,名字必须是 John..

class Member 
{

    private $title;
    private $firstname;

    public function __construct( $title ) 
    {
        $this->title = $title;

    }

    public function showProfile() 
    {
        echo "<dl>";
        echo "<dt>Title:</dt><dd>$this->title</dd>";
        echo "</dl>";
    }
}

$data = new Member( "Mrr" );
$data->showProfile();

【问题讨论】:

  • 您希望将其作为例外还是作为可以返回给用户的字符串?

标签: php oop properties


【解决方案1】:

你可以试试这个,希望对你有帮助。

Try this code snippet here

<?php

ini_set("display_errors", 1);

class Member
{

    private $title;

    public function __construct($title)
    {
        if(!in_array($title, ["Mr","Mrs","Miss"]))
        {
            throw new Exception("Only Mr,Mrs,Miss are allowed!");
            //or you can simple echo out your message instead of exception
        }
        $this->title = $title;
    }

    public function showProfile()
    {
        echo "<dl>";
        echo "<dt>Title:</dt><dd>$this->title</dd>";
        echo "</dl>";
    }

}

$data = new Member("Mrr");

您可以选择使用in the class 为这个error 设置一个变量,以防止进一步执行类脚本的方法。你也可以这样操作

解决方案 2:

Try this code snippet here

<?php

ini_set("display_errors", 1);

class Member
{

    private $title;
    private $error=false;
    public function __construct($title)
    {
        if(!in_array($title, ["Mr","Mrs","Miss"]))
        {
            $this->error=true;
        }
        $this->title = $title;
    }

    public function showProfile()
    {
        if($this->error!==true)
        {
            echo "<dl>";
            echo "<dt>Title:</dt><dd>$this->title</dd>";
            echo "</dl>";
        }
        else
        {
            echo "Only Mr,Mrs,Miss is allowed!";
        }
    }

}

$data = new Member("Mrr");
$data->showProfile();

【讨论】:

  • 谢谢,也感谢 Eval 的介绍,我会经常使用这个!
  • 抱歉,如果有字段,您可以添加吗?例如,标题和名字必须是 James,我正在尝试添加它,它一次只显示一个异常
  • @ALCHI 对不起,我没明白,你能解释一下吗?
  • 我在我的问题中添加了一个名为 firstname 的新属性,所以现在它必须要验证属性。标题必须是先生,夫人,小姐,名字必须是约翰,当我尝试使用一个属性的第一个示例时,它可以正常工作,但是对于两个属性,它只显示一个错误
  • 谢谢@Sahil,这就是我所需要的!
【解决方案2】:

做一个二传手

   function setTitle($newTitle){
      if(in_array($newTitle, array('Mr', 'Miss', 'Mrs' ))
        $this->title=$newTitle;
      else
        echo 'ERROR';
    }

然后从构造函数中调用它

【讨论】:

    【解决方案3】:

    我不喜欢任何答案。 这是我的。我认为您应该在解决方案中使用增变器。成员类应该与 setter 解耦。

    class Member
    {
    
    private $title;
    
    public function setTitle($title)
    {
        $this->title = $title;
    }
    public function showProfile()
    {
        return sprintf("<dl><dt>Title</dt><dt><dd>%s</dd></dt></dl>" , $this->title );
    }
    
    }
    
    
    
    class TitleProperty
    {
    protected $name = 'title';
    protected $allowed_allowed = ['mr', 'mrs', 'miss'];
    
    public $errors = [];
    
    /**
    *@param Member $member
    *@param string $value
    */
    public function __construct( Member $member, $value )
    {
        if(!in_array($value, $this->allowed_allowed )){
            $this->errors[] = "Only Mr,Mrs,Miss is allowed";
        }
        else{
            $member->setTitle( $value );
        }
    }
    }
    
    $member = new Member();
    $property = new TitleProperty($member, 'hello');
    if($property->errors){
    print_r($property->errors);
    }
    else{
    echo 'title set.';
    }
    

    你来了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 2015-12-24
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      相关资源
      最近更新 更多