P
P
Platton2015-09-22 21:20:54
PHP
Platton, 2015-09-22 21:20:54

Sharing a variable across all php classes?

How to make such a task possible:
- for example, there are classes with the same methods - provide. How can I make the $title variable available in each of these class methods ? And also when changing it in one of the methods, did it change for other methods with the same value?
82ac785d42894857afb8dbd08aff0704.jpg

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey, 2015-09-22
Protko @Fesor

class TitleProvider {
    private $title;
    public function getTitle() { return $this->title; }
    public function setTitle($title) { $this->title = $title;}
}

class Foo {
    private $titleProvider;

    public function __construct(TitleProvider $titleProvider){ 
        $this->titleProvider;
    }
}

something like this. Or static. Or even figure out how to make these three classes not depend on this garbage.

D
Dmitry Kim, 2015-09-22
@kimono

Create another class:

class MyVar {
  public static $title;
}

and access the variable:
public function provide(){
  MyVar::$title = 'Hello world!';
}

L
lyeskin, 2015-09-23
@lyeskin

Singleton help?

M
MetaDone, 2015-09-23
@MetaDone

For these purposes, there are traits
php.net/manual/ru/language.oop5.traits.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question