在php中实例化一个私有类
我试图用私有内部变量访问Dog类,我试图在底部实例化这些变量,尽管它运行,但不会将“loud”更改为“loud”,有人能解释为什么会发生这种情况吗 __构造故意留空在php中实例化一个私有类,php,class,private,instantiation,Php,Class,Private,Instantiation,我试图用私有内部变量访问Dog类,我试图在底部实例化这些变量,尽管它运行,但不会将“loud”更改为“loud”,有人能解释为什么会发生这种情况吗 __构造故意留空 class Dog { private $_bark='loud'; //how loud the bark is private $_goodBoy='yes';//how well dog behaves private $_wetnessOfNose='moist';//how wet the dog
class Dog {
private $_bark='loud'; //how loud the bark is
private $_goodBoy='yes';//how well dog behaves
private $_wetnessOfNose='moist';//how wet the dog nose is
public function __construct() {
}
public function bark() {
// retrieves how loud the bark is
return $this->_bark;
}
public function goodboy() {
// retrieves status of dog behavior
return $this->_goodBoy;
}
public function nose() {
// retrieves how wet dogs nose is
return $this -> _wetnessOfNose;
}
}
$myDog= new Dog();
$myDog->bark('LOUD');
echo "myDog's bark is " . $myDog->bark();
您没有更改变量
$\u bark
的值,而是返回一个值。在没有任何可提供的情况下,您正在为函数bark
提供参数
class Dog {
private $_bark='loud'; // how loud the bark is
private $_goodBoy='yes'; // how well dog behaves
private $_wetnessOfNose='moist'; // how wet the dog nose is
public function setBark($newBark){
//sets how loud the bark is
$this->_bark = $newBark;
}
public function bark(){
//retrieves how loud the bark is
return $this->_bark;
}
public function goodboy(){
//retrieves status of dog behavior
return $this->_goodBoy;
}
public function nose(){
//retrieves how wet dogs nose is
return $this -> _wetnessOfNose;
}
}
$myDog= new Dog();
$myDog->setBark('LOUD');
echo "myDog's bark is " . $myDog->bark();
您的bark()
似乎是getter,而不是setter,因此调用
$myDog->bark('LOUD');
这完全没有意义,因为它没有使用返回值+您尝试传递参数,即使没有什么需要它
命名getter(如getBark()
)和setter(如setBark()
)是一种很好的做法,可以避免类似的问题,您应该删除当前的bark()
并引入
public function getBark(){
return $this->_bark;
}
public function setBark($bark){
$this->_bark = $bark;
}
您已经编写了getter,但没有编写setter。要设置私有属性,您需要编写设置该私有属性的类方法 接球手和接球手 您可以编写两种方法,
getBark
和setBark
(可能是最自我记录的方法),前者将返回$\u bark
,后者将获取$bark
参数并将$this->\u bark
设置为$bark
示例:
public function getBark() {
return $_bark;
}
public function setBark($bark) {
$this->_bark = $bark;
}
public function bark($bark) {
if (isset($bark)) { // If you've supplied a $bark parameter...
$this->_bark = $bark; // Set $_bark to the value of the parameter
return; // Exit the method without returning anything (does not continue outside of the if statement
}
return $this->_bark; // No parameter was supplied, so just return the value of $_bark
}
组合吸气剂和setter
或者,您可以结合使用get
和set
方法,只需像当前一样使用bark
方法。它应该使用$bark
参数,并使用PHP检查它是否为null
,然后将$\u bark
设置为$bark
,然后退出该方法而不返回任何值如果参数$bark
未设置则只返回私有属性$\u bark
的值。因此,本质上,它涵盖了这两种方法的职责
此方法可称为$myDog->bark()
以获取$\u bark
的值,或称为$myDog->bark('LOUD');
以将$\u bark
的值设置为'LOUD'
示例:
public function getBark() {
return $_bark;
}
public function setBark($bark) {
$this->_bark = $bark;
}
public function bark($bark) {
if (isset($bark)) { // If you've supplied a $bark parameter...
$this->_bark = $bark; // Set $_bark to the value of the parameter
return; // Exit the method without returning anything (does not continue outside of the if statement
}
return $this->_bark; // No parameter was supplied, so just return the value of $_bark
}
坏习惯
最后,您可以将
\u bark
属性公开,但您确实不应该这样做。您应该在代码中启用错误报告。PHP将抱怨在未编码为接受参数时将参数传递给bark()
。错误报告(E\u ALL);ini\u设置('display_errors',1)
如果您修改bark()
以接受一个参数,并在返回参数之前将$this->\u bark
的值设置为该参数,您将实现您的目标。\u构造故意保留为空
则可以删除它。此外,您可能正在查找\u set()
不确定。您使用参数调用方法bark(),但在方法定义中没有参数(isset($bark)
=>(isset($bark))
请更改。我理解第一个示例,但不太理解第二个示例,有什么可以让我更好地理解发生了什么吗?@Kirtar我稍微更新了我的答案,并做了一些解释。这对你有用吗?谢谢,我使用了这种格式,现在可以了。我现在意识到我没有setter。