Php 如何在方法中声明属性?
我是一个完全用PHP写作的新手,几天前就开始了 我想在一个方法中声明一个新的“公共属性”,以便在其他方法中使用 这就是我所想的(当然没用!)Php 如何在方法中声明属性?,php,methods,global-variables,Php,Methods,Global Variables,我是一个完全用PHP写作的新手,几天前就开始了 我想在一个方法中声明一个新的“公共属性”,以便在其他方法中使用 这就是我所想的(当然没用!) 提前感谢您提供的提示。当您在方法(函数)中定义该类的每个变量时,它都是公共的!您可以通过以下方式执行此操作: class hello { public function b() { $this->c = 20; } public function output() { echo $thi
提前感谢您提供的提示。当您在方法(函数)中定义该类的每个变量时,它都是公共的!您可以通过以下方式执行此操作:
class hello {
public function b() {
$this->c = 20;
}
public function output() {
echo $this->c;
}
}
$new = new hello;
$new->output();
或者让函数b()
返回$c
,然后将其作为变量传递给output()
:
请记住,函数中的所有变量只能从该特定函数中访问……当然,除非您使用$this
,这使变量成为类属性!
另外,建议只返回变量…echo仅用于实际输出、纯HTML、模板和视图,如果您知道我的意思的话:)请尝试以下操作:
class hello {
public $c = null;
public function b() {
$this->c = 20; //I'd like to make $c usable by the method output()
}
public function output() {
echo $this->c;
}
}
同学们好{
公费$c;
公共职能b(){
$this->c=20;//我想让方法output()可以使用$c
}
公共功能输出(){
返回$this->c;
}
}
$new=newhello;
echo$new->output();
注意:如果需要在另一个方法中使用属性,则不需要将该方法声明为public,您应该将该属性声明为private,并且您可以毫无问题地访问您的属性:
class hello {
private $c;
public function b() {
$this->c = 20;
}
public function output() {
$this->c;
}
}
按如下方式重写此代码:-
class hello {
public $c;
public function b() {
public $this->c = 20; //I'd like to make $c usable by the method output()
}
public function output() {
echo $this->c;
}
}
$new = new hello;
$new->output();
我想在一个方法中声明一个新的“公共属性”,以便在其他方法中使用
如果其他方法是同一类的一部分,则不需要公共属性,私有属性将满足您的需要。私有属性只能在同一个类中访问,这有助于保持简单
还要理解声明属性和为其赋值之间的区别。在加载代码时进行声明,在执行代码时进行赋值。因此,声明(或定义)属性(私有或公共)需要在PHP语法中有一个特殊的位置,即在类的主体中,而不是在函数中
您可以使用PHP中的特殊变量$this
访问类内的属性
当从对象上下文中调用方法时,伪变量$this
可用$this
是对调用对象(通常是方法所属的对象)的引用
私人财产示例:
class hello {
private $c; # properties defined like this have the value NULL by default
public function b() {
$this->c = 20; # assign the value 20 to private property $c
}
public function output() {
echo $this->c; # access private property $c
}
}
$new = new hello;
$new->output(); # NULL
$new->b();
$new->output(); # 20
希望这是有帮助的。使用私有属性是因为程序中的所有其他内容都不需要关心它,因此在类中,您知道没有其他内容可以操纵该值。请参见。Simple,
class hello{public$property;}
这不起作用!函数b从未被调用,因此输出将为空。您不必将其设置为null
。谢谢您的回答!非常感谢你的努力。
class hello {
public $c;
public function b() {
$this->c = 20; //I'd like to make $c usable by the method output()
}
public function output() {
return $this->c;
}
}
$new = new hello;
echo $new->output();
class hello {
var $c;
function __construct() { // __construct is called when creating a new instance
$this->c = 20;
}
public function getC() {
return $this->c;
// beter to return a value this way you can later decide what to do with the value without modifying the class
}
}
$new = new hello; // create new instance and thus call __construct().
echo $new->getC(); // echo the value
class hello {
private $c;
public function b() {
$this->c = 20;
}
public function output() {
$this->c;
}
}
class hello {
public $c;
public function b() {
public $this->c = 20; //I'd like to make $c usable by the method output()
}
public function output() {
echo $this->c;
}
}
$new = new hello;
$new->output();
class hello {
private $c; # properties defined like this have the value NULL by default
public function b() {
$this->c = 20; # assign the value 20 to private property $c
}
public function output() {
echo $this->c; # access private property $c
}
}
$new = new hello;
$new->output(); # NULL
$new->b();
$new->output(); # 20