SoFunction
Updated on 2025-03-09

Analysis of static static properties and static method examples of php

This article describes the static static properties and static methods of php. Share it for your reference. The details are as follows:

<?php
/*
 * static
 */
 /*Static: belongs to a class and not to a single object (global, shared by all objects)
  * Static properties: When calling static properties within a class's method, do not use the $this-> method, but use the self:: method.
  * Static method:
  * Can be called when the class does not have any object.
  *It's okay to use it as an ordinary method
  *The normal method cannot be called in static methods
  *
  * */
 class xin {
  static private $name;
  public function setname($namec) {
    self::$name = $namec;
  }
  public function getname() {
    return self::$name;
  }
  static public function name($namecc) {
    echo "I am $namecc";
  }
 }
 $xind = new xin();
 $xind-&gt;setname("Site <br/>");
 echo $xind-&gt;getname();
 $oldd = new xin();
 $oldd-&gt;setname("Government <br/>");
 echo $oldd-&gt;getname();
 echo $xind-&gt;getname();
 echo xin::name("Star");
 echo "&lt;br/&gt;";
?&gt;

The operation results are as follows:

place
government
government
I am Stars

I hope this article will be helpful to everyone's PHP programming.