简体   繁体   English

如何在类中使用变量在PHP中全局更改其值

[英]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.

相关问题 在PHP中,在为类定义公共变量时,如何使用变量作为key =&gt; value对中的值? - In PHP, how can I use a variable as the value in a key=>value pair when defining a public variable for a class? 全局更改javascript变量的值 - Changing value of a javascript variable globally 如何在不更改可见性的情况下访问类中的私有变量 - How can I access a private variable in a class without changing its visibility 我可以使变量全局可见,而不必在每个PHP类的构造函数中声明它是全局的吗? - Can I make a variable globally visible without having to declare it global in every single PHP class's constructor? 如何在不断更改其值的同时访问多个PHP页面上的变量或变量值或变量 - How to access variable or variable value or variable on multiple PHP pages while continuously changing its value 为什么我的本地 PHP 函数看不到 $test 变量,如何获取它的值? - Why can't my local PHP function see the $test variable, and how can I get its value? 选择单选按钮后,如何使用其值动态更新另一个 PHP 变量 - Upon selecting a radio button, how do I use its value to dynamically update another PHP variable 如何在PHP中使用变量变量? - How can I use a variable variable in PHP? PHP:如何在自己的类中访问/使用方法? - PHP: How can I access / use a method within its own class? 我可以通过php中的USE动态变量使用帮助程序类吗? - Can I use a helper class by USE dynamic variable in php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM