[英]How can I use a variable in a class for changing its value globally in PHP
How can I use a variable in a class, such as if I change the value of the variable from member function then its value will be changed globally . 我如何在类中使用变量,例如,如果我从成员函数更改了变量的值,那么其值将被全局更改。 How can I do this?
我怎样才能做到这一点?
i think you are searching for this: 我想你正在寻找这个:
class abcd {
public $value;
function __construct($value) {
$this->value = $value;
}
public function change ($change)
{
$this->value = $change;
}
}
$new= new abcd (123);
print_r($new);
$new->change (546);
print_r($new);
first.php first.php
<?php
class first {
private $name;
function __construct() {
$this->name="name";
}
function changeName(){
$this->name="newName";
}
function getName()
{
return $this->name;
}
}
?>
second.php second.php
<?php
include 'first.php';
$firstObject=new first;
echo $firstObject->getName().'<br>';
$firstObject->changeName();
echo $firstObject->getName();
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.