Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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_Oop_Properties - Fatal编程技术网

在PHP中设置属性值

在PHP中设置属性值,php,oop,properties,Php,Oop,Properties,我有一个叫做members的类,下面有一个例子。我要问的是,我如何设置标题的值。例如,我只允许Mr、Mrs、Miss,任何其他值都将抛出一个错误,声明只允许Mr、Mrs、Miss,Firstname必须是John class Member { private $title; private $firstname; public function __construct( $title ) { $this->title = $title

我有一个叫做members的类,下面有一个例子。我要问的是,我如何设置标题的值。例如,我只允许Mr、Mrs、Miss,任何其他值都将抛出一个错误,声明只允许Mr、Mrs、Miss,Firstname必须是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();
类成员
{
私人产权;
私人$firstname;
公共功能构造($title)
{
$this->title=$title;
}
公共函数showProfile()
{
回声“;
echo“Title:$this->Title”;
回声“;
}
}
$data=新成员(“Mrr”);
$data->showProfile();

您可以尝试一下,希望这会有所帮助

做二传手

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

然后从构造函数调用它

我不喜欢任何答案。 这是我的。我认为你应该在解决方案中使用变异因子。成员类应该与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.';
}
类成员
{
私人产权;
公共函数setTitle($title)
{
$this->title=$title;
}
公共函数showProfile()
{
返回sprintf(“标题%s”,$this->Title);
}
}
类别所有权
{
受保护的$name='title';
受保护的$allowed_allowed=['mr'、'mrs'、'miss'];
公共$errors=[];
/**
*@param成员$Member
*@参数字符串$value
*/
公共函数构造(成员$Member,$value)
{
如果(!在数组中($value,$this->allowed\u allowed)){
$this->errors[]=“只允许先生、太太、小姐”;
}
否则{
$member->setTitle($value);
}
}
}
$member=新成员();
$property=newtitleproperty($member,'hello');
如果($property->errors){
打印($property->errors);
}
否则{
回声“标题集”;
}

好了

你想把它作为一个例外还是一个可以返回给用户的字符串?谢谢,也谢谢你对Eval的介绍,我要用很多!很抱歉,如果有字段,您可以添加吗?比如说,标题和名字必须是James,我正在尝试添加它,它一次只显示一个异常time@ALCHI很抱歉,我没有明白,你能解释一下吗?我在问题中添加了一个名为firstname的新属性,所以现在必须验证属性。标题必须是Mr,Mrs,Miss,名字必须是John,当我用一个属性尝试您的第一个示例时,它工作正常,但用两个属性它只显示OneHank you@Sahil的错误,这就是我所需要的!
   function setTitle($newTitle){
      if(in_array($newTitle, array('Mr', 'Miss', 'Mrs' ))
        $this->title=$newTitle;
      else
        echo 'ERROR';
    }
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.';
}