简体   繁体   中英

Dependency Injection with XML in PHP

I'm currently trying to implement a simple dependency injection container that uses XML to define the class dependencies. The questions I have are mainly related to design.

Here is an example XML schema that I am thinking about using:

<?xml version="1.0">
<classes>
    <class name="MySQLDatabase">
        <namespace>MyLib\Database\</namespace>
        <dependency type="constant">my_db_username</dependency>
        <dependency type="constant">my_db_password</dependency>
        <dependency type="constant">my_db_database</dependency>
    </class>
        <class name="LoginManager">
        <namespace>MyLib\Authentication\</namespace>
        <dependency type="class">MySQLDatabase</dependency>
    </classes>
</classes>

My first question is, does anyone foresee any problems with a schema similar to the above?

My second question is, how should I go about translating this into an my definitions array? This question isn't about how to parse the XML, rather the design of the array that will hold the information once parsed.

If I was coding this in Java, I was thinking of something like this, but I'm not sure if this is too bulky and how this would translate into PHP, which doesn't seem to have the same amount of data structures as Java does.

class DIClass {

    public HashMap<String, ClassDefinition> definitions;

    // example
    // definitions = map of the classes below
    // ...
}

class ClassDefinition {

    private String name;
    private String namespace;
    private List<Dependency> dependencies;

    // example
    // name = MySQLDatabase
    // namespace = MyLib\Database\
    // dependencies = list of the class below, ordered
    // ...
}

class Dependency {

    private String name;
    private String type;

    // example
    // name = my_db_username
    // type = constant
    // ...
}

My last question is, what about adding a definition for persistence? For example, the database class should be a singleton. Should I just add an extra <persis /> tag into the class definition in the XML file?

Thanks for looking.

My first question is, does anyone foresee any problems with a schema similar to the above?

You are re-inventing the wheel. PHP is a scripting language, so instead of an XML file you can as well just use a PHP file - it's much more expressive for - well - PHP :)

My second question is, how should I go about translating this into an my definitions array? This question isn't about how to parse the XML, rather the design of the array that will hold the information once parsed.

You can do that by mocking with an array first and then thinking backwards, like how to store the information you have in the array into an XML file - or just drop the XML completely and just load the array (see the point above).

My last question is, what about adding a definition for persistence? For example, the database class should be a singleton. Should I just add an extra tag into the class definition in the XML file?

If you need them a singleton, instantiate them once and that's it. You don't need singleton persistence in PHP normally. That's a large misconception (if you come from a Java background this gives a good insight: Singletons have very little - if not to say no - use in PHP (a must read)).

See as well the following realted question(s):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM