如何在类中使用变量,例如,如果我从成员函数更改变量的值,则其值将全局更改。我怎么能这样做?
答案 0 :(得分:0)
我认为你正在寻找这个:
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);
答案 1 :(得分:0)
first.php
<?php
class first {
private $name;
function __construct() {
$this->name="name";
}
function changeName(){
$this->name="newName";
}
function getName()
{
return $this->name;
}
}
?>
second.php
<?php
include 'first.php';
$firstObject=new first;
echo $firstObject->getName().'<br>';
$firstObject->changeName();
echo $firstObject->getName();
?>