Coder Social home page Coder Social logo

Variable Expanding about config HOT 2 CLOSED

respect avatar respect commented on July 30, 2024
Variable Expanding

from config.

Comments (2)

alganet avatar alganet commented on July 30, 2024

Actually, the usage is a little bit different. Each [section] creates a new object or array, and variable expanding works only for variables outside sections. I would probably write something like this:

pdo_driver = "mysql"
pdo_host = "localhost"
pdo_name = "somedb"
pdo_user = "root"
pdo_pass = ""
pdo_dsn = "[pdo_driver]:host=[pdo_host];dbname=[pdo_name]"

mongo_driver = "mongodb"
mongo_host = "localhost"
mongo_name = "something"
mongo_user = "user"
mongo_pass = "pass"
mongo_server = "[mongo_driver]://[mongo_user]:[mongo_pass]@[mongo_host]"

[pdoConnection PDO]
dsn = [pdo_dsn]
username = [pdo_user]
password = [pdo_pass]

[mongoConnection Mongo]
server = [mongo_server]

[mongoDb MongoDb]
conn = [mongoConnection]

And then:

<?php

$container = new Respect\Config\Container("config.ini");
$mongoDb = $container->mongoDb;
$db = $container->pdoConnection;

In this sample, the keys outside sections acts as variables that you can use elsewhere. The [sections] represent objects, so [pdoConnection PDO] means $pdoConnection = new PDO. The keys for each section are parameters for that object. They can be constructor params, properties or method calls.

The PDO object for example has a constructor with the params $dsn, $username and $password. Respect\Config then uses the keys inside the config section to instantiate that object.

The usage is similar to PHP itself, but the main advantage is that every object declaration is lazy loaded. The PDO connection will only start when you try to get it from the container.

Let me know if I can help you with anything else. Thanks!

from config.

dre1080 avatar dre1080 commented on July 30, 2024

thanks! :)

from config.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.