Makis
Makis

Reputation: 1254

Too many parameters?

Ok, i have managed to end at this one.

Controller:

  $addProperty=Property::addProperty($title,$description,$location,
                 $agent,$owner,$lat,$long,$position,$photoHolder,
                 $stars,$negatives,$adverts,$dropLink,$photosSite);

Model:

  public static function addProperty($title,$description,$location,$agent,
           $owner,$lat,$long,$position,
           $photoHolder,$stars,$negatives,
           $adverts,$dropLink,$photosSite)

The problem is that, not only, i have too many parameters but i need to pass around 10 more.

Any advice?

Upvotes: 0

Views: 809

Answers (2)

diggersworld
diggersworld

Reputation: 13090

There's quite a few ways you could do this. My preferred way though when working with models is to have a set method for each attribute. That way you don't need to pass everything all at once (very useful as an application evolves and stuff gets added/removed).

So in a model, I would usually have something like this:

class Property {

    private $title;
    private $description;
    private $location;

    /**
     * Creates an instance of property via a static method.
     *
     */
    public static factory()
    {
        return new Property();
    }

    public function setTitle($title)
    {
        $this->title = $title;
        return $this;
    }

    public function setDescription($description)
    {
        $this->description = $description;
        return $this;
    }

    public function setLocation($location)
    {
        $this->location = $location;
        return $this;
    }

    // because the attributes in this instance are private I would also need getters

    public function getTitle()
    {
        return $title;
    }

    public function getDescription()
    {
        return $description;
    }

    public function getLocation()
    {
        return $location;
    }
}

Then you can also add in a save() method or whatever else you want it to do.

OK, so I've added a new static method called factory which allows you to create an instance without having to assign it to a variable. In addition to that I have added return $this; to all methods which do not return an attribute.

What this effectively means is you can now do this:

// create a new property
Property::factory()
    ->setTitle($title)
    ->setDescription($description)
    ->setLocation($location)
    ->save(); // if you had that function

The beauty of this is that if you did need to have a break in it, then the following would also work.

// create a new property
$property = Property::factory()
    ->setTitle($title)
    ->setDescription($description); // this is returning the property instance `return $this`

// do some processing to get the $location value

// continue creating the new property
$property
    ->setLocation($location)
    ->save(); // if you had that function

Upvotes: 2

saeed parhizkary
saeed parhizkary

Reputation: 71

better way is to pass parameter as an array :

$params=array(
'title'=>'title',
'other_parameter'=>'value',
);

$addProperty=Property::addProperty($params);

Upvotes: 2

Related Questions