Question

I have the following class in PHP

class MyClass
{
  // How to declare MyMember here? It needs to be private
  public static function MyFunction()
  {
    // How to access MyMember here?
  }
}

I am totally confused about which syntax to use

$MyMember = 0; and echo $MyMember

or

private $MyMember = 0; and echo $MyMember

or

$this->MyMember = 0; and echo $this->MyMember

Can someone tell me how to do it?

I am kind of not strong in OOPS.

Can you do it in the first place?

If not, how should I declare the member so that I can access it inside static functions?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top