0

could someone explain how to use not static property in static method in php, this is wrong code, but i want to know how to fix this, thank you

<?php
class SomeClass
{
    public $_someMember;

    public function __construct()
    {
        $this->_someMember = 1;
    }

    public static function getSomethingStatic()
    {
        return $this->_someMember * 5; // here's the catch
    }
}

echo SomeClass::getSomethingStatic();
?>
0

3 Answers 3

4

You can't directly. You need to create an object instance. You can make one and pass it to a static method, or make one in static method's body.

Regular (non-static) properties require object instance of given class (type). Static methods are called by referring to the class itself, not an object.

You can however use static properties or constants for static methods needs without creating object instance at all.

Sign up to request clarification or add additional context in comments.

5 Comments

"You can't" is a bit misleading. For example, Laravel's Eloquent ORM makes heavy use of non-static properties in static method calls.
@Marty - Edited. I wonder however what is the point of creating object instance in a static method? Why not just make an instance in the first place, and use dynamic methods of that instance?
With the Eloquent ORM for example, the base class for all models has a bunch of static methods for getting objects from the db. When you create a class that extends this, you can add some properties like the primary key name etc. From there you can use YourModel::something() and the base class will know to make an instance of YourModel, then get any important info from it for use in whatever you are trying to do.
@Marty - doesn't this break the SOLID Single Responsibility Principle? One class both represents a model and handles retrival from database. This couples your models with the ORM library you mentioned, right? The coupling is as strongest as it can get - inheritance. I think there should be at least two classes here. One - representing the model (ORM agnostic). Second - that would retrieve data from database and put them in model instances - a concrete Eloquent ORM class implementing ORM agnostic interface.
Sure, I never said I agree with the way it works or even enjoy using it - it's just the first example I thought of that was relevant for this thread.
0

You have to instantiate object

<?php
class SomeClass
{
    public $_someMember;

    public function __construct()
    {
        $this->_someMember = 1;
    }

    public static function getSomethingStatic()
    {
        $object = new self();
        return $object->_someMember * 5; // here's the catch
    }
}

echo SomeClass::getSomethingStatic();

Comments

0

You can statically create an instance of the class that the method is being called on via:

$instance = new static();

You can also statically create instances of the class that actually defines the method via:

$instance = new self();

As an example, take these classes First and Second.

class First
{
    public static function getStatic()
    {
        return new static();
    }

    public static function getSelf()
    {
        return new self();
    }
}


class Second extends First{ }
  • When we use Second::getStatic(), we will get an instance of Second.
  • When we use Second::getSelf(), we will get an instance of First.
  • When we call either method via First, we will get an instance of First.

This means you can change your method to:

public static function getSomethingStatic()
{
    $instance = new static(); // or new self() if you always want to use 'SomeClass'
                              // and never an extending class.

    return $instance->_someMember;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.