Home >Backend Development >PHP Tutorial >PHP learning journey: static variables and methods
The
static keyword is used to modify properties and methods, and these properties and methods are called static properties and static methods.
Static methods can only access static properties and cannot access non-static properties. However, when calling non-static methods, you cannot use the this keyword to call non-static methods, but must use the self:: keyword, and the non-static method being called cannot have non-static variables, in general Static methodsTry not to call nonstatic methods.
static attributes have only one copy in memory and are shared by all instances.
You can use the self:: keyword to access static members of the current class.
non static method call static variable
<code><span><?php</span><span><span>class</span><span>test</span>{</span><span>public</span><span>static</span><span>$pi</span>=<span>3.14</span>; <span><span>function</span><span>display</span><span>()</span> {</span><span>return</span><span>self</span>::<span>$pi</span>; } } <span>$test</span>=<span>new</span> test(); <span>echo</span><span>'<br/>'</span>.<span>$test</span>->display(); <span>?></span></code>
static method call static variable
<code><span><?php</span><span><span>class</span><span>test</span>{</span><span>public</span><span>static</span><span>$pi</span>=<span>3.14</span>; <span>static</span><span><span>function</span><span>display_static</span><span>()</span> {</span><span>return</span><span>self</span>::<span>$pi</span>; } } <span>$test</span>=<span>new</span> test(); <span>echo</span><span>'<br/>'</span>.<span>$test</span>::display_static(); <span>?></span></code>
non static method call static method
<code><span><?php</span><span><span>class</span><span>test</span>{</span><span>public</span><span>static</span><span>$pi</span>=<span>3.14</span>; <span>static</span><span><span>function</span><span>display_static</span><span>()</span> {</span><span>return</span><span>self</span>::<span>$pi</span>; } <span><span>function</span><span>display</span><span>()</span> {</span><span>return</span><span>self</span>::display_static(); } } <span>$test</span>=<span>new</span> test(); <span>echo</span><span>'<br/>'</span>.<span>$test</span>->display(); <span>?></span></code>
static method call non Static methods (Actually, it is equivalent to converting non-static methods into static methods during the calling process)
<code><span><?php</span><span><span>class</span><span>test</span>{</span><span>public</span><span>static</span><span>$pi</span>=<span>3.14</span>; <span>static</span><span><span>function</span><span>display_static</span><span>()</span> {</span><span>return</span><span>self</span>::display(); } <span><span>function</span><span>display</span><span>()</span> {</span><span>return</span><span>self</span>::<span>$pi</span>; } } <span>$test</span>=<span>new</span> test(); <span>echo</span><span>'<br/>'</span>.<span>$test</span>::display_static(); <span>?></span></code>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
The above introduces the PHP learning journey: static variables and methods, including static methods, static properties, and static variables. I hope it will be helpful to friends who are interested in PHP tutorials.