oop - Sharing objects between PHP classes -


What is the best way to share things between other classes?

For example; A "database" object with the tasks required by "article" and "user" objects.

I do not want to use globals (which is also a singleton) or a new instance of the object in each class, such as

  function __construct () {$ this - & gt; Database = new database; $ This- & gt; Cache = new cache; }  

will have to pass through objects, e.g.

  class test {function __construct ($ obj) {$ this-> obj = $ obj; }} $ Database = new database; $ Test = new test ($ database);  

What can be the way to go?

Yes. Passing objects around the constructor - or setter - the best way to go is known as this pattern. There is an added benefit in it that makes it easy to test your code (using stubs or socks).


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -