PHP Classes

Documentation, example, or more info

Recommend this page to a friend!

      PHP IOC  >  All threads  >  Documentation, example, or more info  >  (Un) Subscribe thread alerts  
Subject:Documentation, example, or more info
Summary:More information about PHP IOC
Author:Blake Craig
Date:2006-11-10 09:48:44
Update:2011-04-13 07:28:14

  1. Documentation, example, or more info   Reply   Report abuse  
Blake Craig - 2006-11-10 09:48:44
Can someone give me some more information about PHP IOC, or Inversion Control? The PHP IOC framework does not appear to have any documentation or working examples.


  2. Re: Documentation, example, or more info   Reply   Report abuse  
John Paul de Guzman - 2006-11-10 19:13:22 - In reply to message 1 from Blake Craig
Sorry, I'm still formalizing the documentation for PHP IOC. But here's a simple example but working example on how to use the IOC

Creating class or components
class Product {
var $productName;
var $productDescription;
var $productPrice;

function getShippingCost() {}

class ProductManager {
var $products;

// default constructor
function ProductManager() {
$this->products = array();

function getProducts() {}

Having this two class normally classes in PHP can utilized in this manner.
// create a new product: Chair
$chair = new Product();
$chair->productName = "Comfy Chair";
$chair->productDescription = "Comfortable chair for your living room";
$chair->productPrice = 350.89;
// create a new product: Table
$table = new Product();
$table->productName = "Classy Table";
$table->productDescription = "Classy table for your dining room";
$table->productPrice = 500;

// Create the Product Manager
$productManager = new ProductManager();
// add the chair to the collection
$productManager->products[0] = $chair;
// add the table to the collection
$productManager->products[1] = $table;

These is the classic way of accessing and utilizing the class created in PHP (there are lots of different ways i guess).

IOC can be used in simplfiying how the objects in your application are being wired. These definitions and class behaviors can be configured using an XML.

using the XML as configuration: config.xml
<?xml version="1.0"?>
<!-- Product Defintion -->
<object name="myChair"
<property field="productName">Comfy Chair</property>
<property field="productDescription">
Comfortable chair for your living room
<property field="productPrice">350.89</property>
<object name="myTable"
<property field="productName">Classy Table</property>
<property field="productDescription">
Classy table for your dining room
<property field="productPrice">500</property>

<!-- Product Manager -->
<object name="productManager"
<property field="products" type="hash">
<map key="ch01" type="object">myChair</map>
<map key="tb01" type="object">myTable</map>

This XML simply acts as a configuration file for your class or libraries without actually hard coding class relationship. Therefore using IOC lets you develop reusable classes without explicitly implementing class relationship. All relationship are wired inside the XML. By using this class library, all you have to do is to focus on developing your class and its internal behavior letting the XML configuration do the wiring and connections.

Accessing the IOC framework
// PHP IOC Container
// class the required classes needed to make things work
require_once "libs/DependencyContainer.php";

// initialize the object factory
// set the path of the xml configuration file
$oFactory = new ObjectFactory("config/config.xml");

// returns the myTable object

// returns the myChair object

// returns the productManager object
$pm = $oFactory->getObject("productManager");

// returns the myChair object

All instantiation and class wiring are done during runtime execution. However, after the execution, classes are then cached to prevent reinstantiation. This method enhances the speed of the application.

Ill be posting more documentation.
If you have questions just feel free to ask.

  3. Re: Documentation, example, or more info   Reply   Report abuse  
Blake Craig - 2006-11-12 08:05:17 - In reply to message 2 from John Paul de Guzman
Thank you for the detailed information. I have been looking around for a framework in which you can declare the field properties and relationships in a single centralized method. You're approach does this exceptionally well. I am looking forward to your progress on the framework. Great work!

  4. Re: Documentation, example, or more info   Reply   Report abuse  
John Paul de Guzman - 2006-11-12 08:29:47 - In reply to message 3 from Blake Craig
I've been using this framework for almost all my projects. I have an MVC developed using this one. However its written in PHP5. There are different ways you can use the framework. Ill be posting more example if necessary.


  5. Re: Documentation, example, or more info   Reply   Report abuse  
Surekha Matte - 2010-10-07 10:14:21 - In reply to message 2 from John Paul de Guzman
Hello BlackCraig,

I need some examples/documentation how to impliment this IOC in an application. please help me out

  6. Re: Documentation, example, or more info   Reply   Report abuse  
chintan patel - 2011-04-13 07:28:14 - In reply to message 5 from Surekha Matte
i also want to use this class in my application can you share some example or doc with me so i can understand and use this ioc