Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a private member inside a static function in PHP

People also ask

How can I access private static variable in PHP?

Accessing the value of a static variable is similar to accessing for a class constant: You either use the type name (outside of or within the class) or the keyword self , both followed by the scope resolution operator ( :: ) and the name of the static variable, starting with $ .

Can static member functions access private members?

A static member function has the same access rights as a non static member function. So yes, it can access any public, protected, and private variable in the class. However you need to pass an instance of the class to the function for the function to be able to access the member.

Can a static method be private PHP?

A private static method can be called from a public method - static or not - but not from outside the class.

How do you access a private static variable?

A variable declared private static could easily be accessed, but only from the inside of the class in which it is defined and declared. It is because the variable is declared private, and private variables are not accessible outside the class. Within the class, they can be accessed using ClassName. Variable_name .


class MyClass
{
  private static $MyMember = 99;

  public static function MyFunction()
  {
    echo self::$MyMember;
  }
}

MyClass::MyFunction();

see Visibility and Scope Resolution Operator (::) in the oop5 chapter of the php manual.


This is a super late response but it may help someone..

class MyClass
{
  private $MyMember;

  public static function MyFunction($class)
  {
    $class->MyMember = 0;
  }
}

That works. You can access the private member that way, but if you had $class you should just make MyFunction a method of the class, as you would just call $class->MyFunction(). However you could have a static array that each instance is added to in the class constructor which this static function could access and iterate through, updating all the instances. ie..

class MyClass
{
  private $MyMember;
  private static $MyClasses;

  public function __construct()
  {
    MyClass::$MyClasses[] = $this;
  }

  public static function MyFunction()
  {
    foreach(MyClass::$MyClasses as $class)
    {
      $class->MyMember = 0;
    }
  }
}

Within static methods, you can't call variable using $this because static methods are called outside an "instance context".

It is clearly stated in the PHP doc.


<?php
    class MyClass
    {
        // A)
        // private $MyMember = 0;

        // B)
        private static $MyMember = 0;

        public static function MyFunction()
        {
            // using A) //  Fatal error: Access to undeclared static property: 
            //              MyClass::$MyMember
            // echo MyClass::$MyMember; 

            // using A) // Fatal error: Using $this when not in object context
            // echo $this->MyMember; 

            // using A) or B)
            // echo $MyMember; // local scope

            // correct, B) 
            echo MyClass::$MyMember;
        }
    }

    $m = new MyClass;
    echo $m->MyFunction();
    // or better ...
    MyClass::MyFunction(); 

?>

Static or non-static?

Did you ever asked yourself this question?

You can not access non static parameters / methods from inside static method (at least not without using dependency injection)

You can however access static properties and methods from with in non-static method (with self::)

Properties

Does particular property value is assign to class blueprint or rather to it instance (created object from a class)? If the value is not tight to class instance (class object) then you could declare it as as static property.

private static $objectCreatedCount; // this property is assign to class blueprint
private $objectId; // this property is assign explicitly to class instance

Methods

When deciding on making a method static or non-static you need to ask yourself a simple question. Does this method need to use $this? If it does, then it should not be declared as static.

And just because you don't need the $this keyword does not automatically mean that you should make something static (though the opposite is true: if you need $this, make it non-static).

Are you calling this method on one individual object or on the class in general? If you not sure which one to use because both are appropriate for particular use case, then always use non-static. It will give you more flexibility in future.

Good practice is to always start to design your class as non-static and force static if particular us case become very clear.

You could try to declare your parameters as static... just so you can access it from static method but that usually is not what you want to do. So if you really need to access $this from static method then it means that you need to rethink/redesign your class architecture because you have don it wrong.