Skip to content
Snippets Groups Projects

Static properties in classes

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Thomas Flori
    Edited
    statics.php 523 B
    <?php
    
    class Anything
    {
        public static $current = 'initial value';
        
        protected $value;
        
        /**
         * Anything constructor.
         *
         * @param $value
         */
        public function __construct($value)
        {
            $this->value = $value;
        }
        
        public function select()
        {
            self::$current = $this->value;
        }
    }
    
    var_dump(Anything::$current);
    
    $o1 = new Anything('object 1');
    $o2 = new Anything('object 2');
    
    $o1->select();
    var_dump($o1::$current);
    
    $o2->select();
    var_dump($o1::$current);
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment