ALCHI
ALCHI

Reputation: 75

Setting the value of properties in PHP

I have a class called members, i have an example below. What i am asking is how do i set the values of title. So for example , i only allow Mr, Mrs, Miss and any other values will throw out an error stating Only Mr,Mrs,Miss is allowed , Firstname must be 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();

Upvotes: 1

Views: 155

Answers (3)

A H Bensiali
A H Bensiali

Reputation: 875

I didnt like any of the answers. Here's mine. I think you should use a mutator in your solution. The member class should be decoupled from the 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.';
}

There you go

Upvotes: 0

Sahil Gulati
Sahil Gulati

Reputation: 15141

You can try this , hope this will be helpful.

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");

Optionally you can set a variable for this error with in the class, which prevent further execution of methods of class script. You can also do it like this

Solution 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();

Upvotes: 2

Riccardo Bonafede
Riccardo Bonafede

Reputation: 606

Make a setter

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

And then call it from the constructor

Upvotes: 1

Related Questions