Assigning static var to non-static var, method for returning values ​​not working

I am new to PHP and practice using static variables. I decided to take the example that I learned in C ++ and rewrite it for PHP (an example from the bottom of this article ).

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

<?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 ping on line 9 to find out if m_nID is getting the value that I see:

 m_nID: 1 m_nID: 2 m_nID: 3 

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

Edit: both answers still solved this, I would like to β€œcheck” both of them, so thanks! I will leave the source code in the question as it is for any future people who have a similar problem.

+6
source share
2 answers

Your background in C ++ led to this problem, which is easy to do. In PHP, all instance (or object) variables refer to the variables $this-> and static (or class) using self:: . Based on your code:

 public function GetID() { return $m_nID; } 

Access to the private variable $m_nID should be copied as follows:

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

And inside your constructor:

 $m_nID = self::$s_nIDGenerator++; 

Must be:

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

Q and A

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

The above two ways to reference instance and class variables have a completely different kind of syntax:

  • $this is a reference instance variable, and any properties are accessible using the -> operator; $ not repeated for the property names themselves, although they are present in the declaration (for example, private $myprop ).

  • self:: is a synonym for Something:: (the class name itself); it does not reference an instance variable and therefore does not have $ in front of it. To distinguish static variables from class constants ( self::MYCONST ) and class methods ( self::myMethod() ), with the $ prefix.

Extra

Thus, $this->$myvar also accepted and works as follows:

 private $foo = 'hello world'; function test() { $myvar = 'foo'; echo $this->$foo; // echoes 'hello world' } 
+9
source
 class Something{ private static $s_nIDGenerator = 1; private $m_nID; public function Something() { $this->m_nID = self::$s_nIDGenerator++; } public function GetID() { return $this->m_nID; } } 

It is interesting to note the difference between using self::$s_nIDGenerator on a static variable vs using $this->s_nIDGenerator for a static variable, while $this-> will not save anything.

+2
source

All Articles