douhan8581 2012-12-20 03:37
浏览 15
已采纳

将静态var分配给非静态var,返回不起作用的值的方法

I'm new to PHP and practicing using static variables. I decided to grab an example that I learnt from C++ and re-write it for PHP (example from the bottom of this article).

There's a class with two private variables (one static), a constructor and a get-method. The constructor assigns the static variable's value to the second private variable, and then increments.

<?php
class Something
{
    private static $s_nIDGenerator = 1;
    private $m_nID;

    public function Something() { 
       $m_nID = self::$s_nIDGenerator++;
       echo "m_nID: " . $m_nID . "</br>"; //for testing, can comment this out
    }
    public function GetID() {
        return $m_nID;
    }

}

// extra question:
// static variable can be assigned a value outside the class in C++, why not in PHP?
// Something::$s_nIDGenerator = 1; 

    $cFirst = new Something();
    $cSecond = new Something();
    $cThird = new Something();

    echo $cFirst->GetID() . "</br>";
    echo $cSecond->GetID() . "</br>";
    echo $cThird->GetID() . "</br>";
?>

Using the echo test in line 9 to see if m_nID is getting a value I see:

m_nID: 1
m_nID: 2
m_nID: 3

But these values are not being returned by the "->GetID()" calls. Any ideas why?

Edit: both replies so far have solved this, I wish I could "check" them both, so thank you! I'll leave the original code in the question as-is for any future people who have a similar problem

  • 写回答

2条回答 默认 最新

  • douwenan9849 2012-12-20 03:38
    关注

    Your background in C++ led up to this issue, which is an easy mistake to make. In PHP, all instance (or object) variables are referenced using $this->, and static (or class) variables with self::. Based on your code:

    public function GetID() {
        return $m_nID;
    }
    

    Access to the private variable $m_nID should be scoped like this:

    public function GetID() {
        return $this->m_nID;
    }
    

    And inside your constructor:

    $m_nID = self::$s_nIDGenerator++;
    

    It should have been:

    $this->m_nID = self::$s_nIDGenerator++;
    

    Q & A

    Why is there no need to put $ before m_nID when using $this->

    The above two ways of referencing instance and class variables come with a very different kind of syntax:

    1. $this is the instance reference variable and any properties are accessed using the -> operator; the $ is not repeated for the property names themselves, although they're present in the declaration (e.g. private $myprop).

    2. self:: is synonymous to Something:: (the class name itself); it doesn't reference an instance variable and therefore has no $ in front of it. To differentiate static variables from class constants (self::MYCONST) and class methods (self::myMethod()) it's prefixed with a $.

    Extra

    That said, $this->$myvar is accepted too and works like this:

    private $foo = 'hello world';
    
    function test()
    {
        $myvar = 'foo';
        echo $this->$foo; // echoes 'hello world'
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?