1

How to declare static function with array and pass next into other class?

class A
{
    //static
    public static $data = ['Los Angeles', 'New York', 'Washington'];
    //const
    const DATA = ['Los Angeles', 'New York', 'Washington'];
}

Which approach is better and why? Maybe for string or int better is other approach? Which? I don't want to change these values.

I would like use this array in other class:

class B
{
    public function method()
    {
        $data1 = A::$data;
        $data2 = A::DATA;
    }
}

But is this the correct approach in object-oriented programming?

In this case, class B does not know that it uses class A, so perhaps class A should be passed in the method, but maybe this approach doesn't apply when it comes to static methods?

closed as unclear what you're asking by HorusKol, amon, BobDalgleish, 8bittree, gnat Dec 21 '17 at 7:49

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1

There are two questions here:

1.)

First, let's lock at the main difference between the two: The public static $datavariable can be changed anytime after its initialization (even from outside the class). The const DATA not:

class B {
  public function method() {
    A::$data = ['Berlin']; // OK
    A::DATA = ['Berlin'];  // Error
  }
}

If the value should not be changed after the inialization the const keyword should be used. Regarding the type of the value, there is no difference between the two.

(By the way, it is better to use getter/setter methods instead of public variables.)

2.)

A strict interpretation of the object-oriented philosophy could be that there are no static variables/methods: Only instances of classes are allowed, which are passed as parameters. But this is not practicable in the real world. There is nothing wrong with using A::DATA inside other classes.

Furthermore, PHP has no concept of instance final variables like other programming languages. A private const will always be at the class level...

0

Exposing constant could be fine until some behavior is needed. So this behavior more probably than not will be implemented in a client class, instead of where it should be -- in a Cities class. People are generally lazy, so I'd better start with the right approach from the very beginning. Your behavior will be encapsulated in an object, and you can modify it with decorators. Plain data is not composable, and exposing it means being procedural. So here is how it could look like:

interface Cities
{
    public function list();
}

class DefaultCities implements Cities
{
    public function list()
    {
        return ['Los Angeles', 'New York', 'Washington', 'London', 'Paris'];
    }
}

class AmericanCities implements Cities
{
    private $cities;
    private $dictionary;

    public function __construct(AmericanCitiesDictionary $dictionary, Cities $cities)
    {
        $this->dictionary = $dictionary;
        $this->cities = $cities;
    }

    public function list()
    {
        return
            array_filter(
                $this->cities->list(),
                function ($city) {
                    return $this->dictionary->exist($city);
                }
            );
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.