问题描述
我一直在网上搜索此内容,但似乎找不到清晰易懂的内容。我在Java上看到了与此类似的类似问题。
I have been searching for this online, but I can't seem to find something that is clear enough for me to understand. I have seen "similiar" questions on here about this in Java.
class animal{
private $name;
// traditional setters and getters
public function setName($name){
$this->name = $name;
}
public function getName(){
return $this->name;
}
// animal constructors
function __construct(){
// some code here
}
// vs
function __construct($name){
$this->name = $name;
echo $this->name;
}
}
$dog = new animal();
$dog->setName("spot");
echo $dog->getName();
// vs
$dog = new animal("spot");
- 我应该通过setter和getter或通过以下方法声明和访问我的私有字段
- 哪个是最佳实践?
- 我理解构造函数的目的(也许不是),但是这样做的目的是什么?有一个构造函数,是否可以通过setter和getter声明和访问我的私有字段?
请注意...这是我第一次将OOP与Web开发和PHP结合使用,我正在尝试通过学习通过编写一些代码使我的手肮脏,以使我了解OOP中的某些内容。请保持简单。
Please note...this is my first time using OOP with web development and PHP, and I'm trying to learn by getting my hands "dirty" by writing some code in order for me to understand certain things in OOP. Please keep it simple.
推荐答案
更多的是语义问题,而不是每个人的最佳做法。
It is more a matter of semantics than best practice per say.
在您的示例中,您的业务逻辑可能会确定动物总是需要一个名称。
因此,用名称构造对象是有意义的。如果您不想允许
的动物名称被更改,那么您就不用写二传手了。
In your example, your buisness logic may determine that an animal always needs a name.So it makes sense to construct the object with a name. If you do not want to allowan animal's name to be changed, then you don't write a setter.
ie
class Animal
{
private $name;
public function __construct($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
您可能拥有动物没有的其他属性必须拥有,就像所有者
一样,您只能为以下内容写一个吸气剂/设置器:
You may have other properties that an animal doesn't have to have, like an ownerthat you only write a getter/setter for i.e.
class Animal
{
private $name;
private $owner;
public function __construct($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function setOwner($owner)
{
$this->owner = $owner
}
}
但是,如果您发现总是在与所有者
同时创建动物的情况下,为了方便起见,可能希望将其放在构造函数签名中
But if you find that you are always creating an animal with an owner at the same timeyou may want to put that in the contructor signature for convenience
class Animal
{
private $name;
private $owner;
public function __construct($name, $owner = null)
{
$this->name = $name;
$this->owner = $owner;
}
public function getName()
{
return $this->name;
}
public function setOwner(Owner $owner)
{
$this->owner = $owner
}
public function getOwner()
{
return $this->owner;
}
}
如果所有者是您申请中的另一类,则您可以键入提示,表明您的构造函数
需要特定类型(类)的所有者。所有这些都是为了使您或其他开发人员更容易理解代码背后的某些要求/逻辑-并可能在此处或那里捕获错误
If the owner is another class in your application, you can type hint that your constructorneeds an owner of a specific type (class). All of this is used to make it easier for you, or another developer to understand some of the requirements/logic behind your code - as well as potentially catching a bug here or there
class Owner
{
private $name;
public function __construct($name)
{
$this->name = $name;
}
}
class Animal
{
private $name;
private $owner;
public function __construct($name, Owner $owner = null)
{
$this->name = $name;
$this->owner = $owner;
}
public function getName()
{
return $this->name;
}
public function setOwner(Owner $owner)
{
$this->owner = $owner
}
public function getOwner()
{
return $this->owner;
}
}
// Create a new owner!
$dave = new Owner('Farmer Dave');
// a standard php empty object
$otherObj = new \stdClass();
// Create a new animal
$daisy = new Animal('Daisy');
// Farmer dave owns Daisy
$daisy->setOwner($dave);
// Throws an error, because this isn't an instance of Owner
$daisy->setOwner($otherObj);
// Set up Maude, with Dave as the owner, a bit less code than before!
$maude = new Animal('Maude', $dave);
这篇关于PHP设置器/字母和构造器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!