簡體   English   中英

PHP如何在類中調用/保存公共變量

[英]PHP how to call/save a public variable in a class

我有這個例子:

    class test_names {
        public name = 'ralph';

        function dispname() {
            return $this->name;
        }
    }

$test = new test_names;
$test->name = 'John';
$test->dispname;

現在結果將是“ John”,因為我將其保存在de public var中

現在,我有另一個名為test2.php的文件,我這樣做:

    $test = new test_names;
    $test->dispname;

結果將是“ ralph”,但我不希望那樣,我希望它是“ John”(我之前設置的值)

我怎樣才能做到這一點?

不要像以前那樣再次實例化班級:

更換

   $test = new test_names;
   $test->dispname;

通過

$test->dispname;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM