Php 参数太多?
好的,我已经设法在这一个结束 控制器:Php 参数太多?,php,laravel,Php,Laravel,好的,我已经设法在这一个结束 控制器: $addProperty=Property::addProperty($title,$description,$location, $agent,$owner,$lat,$long,$position,$photoHolder, $stars,$negatives,$adverts,$dropLink,$photosSite); public static function ad
$addProperty=Property::addProperty($title,$description,$location,
$agent,$owner,$lat,$long,$position,$photoHolder,
$stars,$negatives,$adverts,$dropLink,$photosSite);
public static function addProperty($title,$description,$location,$agent,
$owner,$lat,$long,$position,
$photoHolder,$stars,$negatives,
$adverts,$dropLink,$photosSite)
型号:
$addProperty=Property::addProperty($title,$description,$location,
$agent,$owner,$lat,$long,$position,$photoHolder,
$stars,$negatives,$adverts,$dropLink,$photosSite);
public static function addProperty($title,$description,$location,$agent,
$owner,$lat,$long,$position,
$photoHolder,$stars,$negatives,
$adverts,$dropLink,$photosSite)
问题是,我不仅有太多的参数,而且还需要再传递大约10个
有什么建议吗 有很多方法可以做到这一点。不过,当使用模型时,我更喜欢的方法是为每个属性设置一个方法。这样,您就不需要一次传递所有内容(随着应用程序的发展和内容的添加/删除,这非常有用) 所以在模型中,我通常会有这样的东西:
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;
}
}
然后,您还可以添加save()
方法或您希望它执行的任何其他操作
好的,我添加了一个新的静态方法,名为factory
,它允许您创建一个实例,而无需将其分配给变量。除此之外,我还添加了return$this
到所有不返回属性的方法
这实际上意味着您现在可以这样做:
// create a new property
Property::factory()
->setTitle($title)
->setDescription($description)
->setLocation($location)
->save(); // if you had that function
这样做的好处是,如果你确实需要休息一下,那么下面的方法也可以
// 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
有很多方法可以做到这一点。不过,当使用模型时,我更喜欢的方法是为每个属性设置一个方法。这样,您就不需要一次传递所有内容(随着应用程序的发展和内容的添加/删除,这非常有用) 所以在模型中,我通常会有这样的东西:
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;
}
}
然后,您还可以添加save()
方法或您希望它执行的任何其他操作
好的,我添加了一个新的静态方法,名为factory
,它允许您创建一个实例,而无需将其分配给变量。除此之外,我还添加了return$this
到所有不返回属性的方法
这实际上意味着您现在可以这样做:
// create a new property
Property::factory()
->setTitle($title)
->setDescription($description)
->setLocation($location)
->save(); // if you had that function
这样做的好处是,如果你确实需要休息一下,那么下面的方法也可以
// 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
更好的方法是将参数作为数组传递:
$params=array(
'title'=>'title',
'other_parameter'=>'value',
);
$addProperty=Property::addProperty($params);
更好的方法是将参数作为数组传递:
$params=array(
'title'=>'title',
'other_parameter'=>'value',
);
$addProperty=Property::addProperty($params);
传递数组或对象。好的,听起来是最合适的方式!另外,我不知道如何使用/设计,但是
addProperty()
似乎应该添加一个:function addProperty($property,$value)
或者function addProperties($propArray)
或者用于多个应用的东西。@abracadver是,addProperty()
向表中添加一行属性
,并在中间的表中添加多对多关系。错了吗?只有不起作用才是错的。然而,一个进入你的角色的程序员能在一分钟内弄清楚那里发生了什么吗?传递一个数组或对象。好的,听起来是最合适的方式!另外,我不知道如何使用/设计,但是addProperty()
似乎应该添加一个:function addProperty($property,$value)
或者function addProperties($propArray)
或者用于多个应用的东西。@abracadver是,addProperty()
向表中添加一行属性
,并在中间的表中添加多对多关系。错了吗?只有不起作用才是错的。然而,一个进入你的角色的程序员能在一分钟内弄清楚那里发生了什么吗?回答得好!我使用静态函数
调用模型而不创建实例。我是面向对象php
的新手,请您解释一下为什么不使用静态函数更好?我已经更新了我的答案,以反映我在应用程序中拥有的功能。静态函数很适合做一次性的事情,或者作为辅助函数使用。但是,当您使用模型时,您通常希望将其保留一段时间,以便可以对其执行各种操作。在您的例子中,您希望添加更多属性,这意味着您必须向静态函数中注入更多变量,从而使其膨胀。非常感谢您的回答!除了get()函数的用法,我什么都懂。原因是什么?因为我已经将变量定义为private,这意味着它们只能在类的范围内使用,所以您不能在外部调用它们。通过添加get方法,您将能够获得像so$property->getTitle()这样的属性代码>。通过限制对变量的访问并强制使用setter和getter,它增加了一层开发人员保护,因为您更清楚地知道何时获取/设置这些属性!非常好的回答和解释。非常感谢你!回答得好!我使用静态函数
调用模型而不创建实例。我是面向对象php
的新手,请您解释一下为什么不使用静态函数更好?我已经更新了我的答案,以反映我在应用程序中拥有的功能。静态函数很适合做一次性的事情,或者作为辅助函数使用。但是,当您使用模型时,您通常希望将其保留一段时间,以便可以对其执行各种操作。在您的例子中,您希望添加更多属性,这意味着您必须向静态函数中注入更多变量,从而使其膨胀。非常感谢您的回答!除了get()函数的用法,我什么都懂。原因是什么?因为我已经将变量定义为private,这意味着它们只能在类的范围内使用,所以您不能在外部调用它们。通过添加get方法,您将能够获得像so$property->getTitle()这样的属性代码>。通过限制对变量的访问并强制使用setter和getter,它增加了一层开发人员保护,因为您更清楚地知道何时获取/设置这些属性!非常好的回答和解释。非常感谢你!