Use a factory of drivers in PHP and break dependencies for its use, despite the include or require

1

I have a difficult doubt to explain.

Suppose I have a class in php:

file: classA.php

<? 
claseA {
    //mi codigo de la clase
}

and I have a factory that creates the instance:

file: myFactory.php

<? include 'claseA.php'
miFactory {
    //getInstanceOfA () {
    //codigo para retornar una instancia unica de A...
}

Now, to use the factory that returns class A, I must include the factory, suppose:

index.php

<? require_once 'myFactory.php';

$factory = new Factory();
$factory->getInstanceOfA(); //un codigo que funciona 

THE PROBLEM:

In this index, having the require of each file, You could also do:

$instanciaDeAsinUsarFactory = new claseA(); // y tambien funciona!!!

How can I break those visibilities despite the need for include?

    
asked by jose 09.04.2018 в 18:45
source

1 answer

0

It's not about the autoload (and try that)

It is that each include of another include makes the whole code "visible" from places where it should not be

The includes and the require declare them at the beginning of each php script, as a palliative what I did was in the getInstanceOfClaseA () {// include ControllerClaseA.php}

So that way, you must if or if you request the instance to include the file that contains the controllerclassA

Likewise, I do not know if it is the most "correct" solution in terms of the architecture of the solution (a factory with a single instance that provides a single instance of each controller with a getInstance for each controller, the index.php file should only know the Interface to know the metodos met for that interface and obtain the instance of the Factory)

It is a question of design, since it works but it should not work if it is not by means of obtaining the instance provided by the factory. Explain well?

Like thank you very much

    
answered by 09.04.2018 в 23:34