|
 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.
Thanks
 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
object/Product.php
class Product {
var $productName;
var $productDescription;
var $productPrice;
function getShippingCost() {}
}
object/ProductManager.php
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"?>
<config>
<!-- Product Defintion -->
<object name="myChair"
classname="object.Product"
type="object">
<properties>
<property field="productName">Comfy Chair</property>
<property field="productDescription">
Comfortable chair for your living room
</property>
<property field="productPrice">350.89</property>
</properties>
</object>
<object name="myTable"
classname="object.Product"
type="object">
<properties>
<property field="productName">Classy Table</property>
<property field="productDescription">
Classy table for your dining room
</property>
<property field="productPrice">500</property>
</properties>
</object>
<!-- Product Manager -->
<object name="productManager"
classname="object.ProductManager"
type="object">
<properties>
<property field="products" type="hash">
<map key="ch01" type="object">myChair</map>
<map key="tb01" type="object">myTable</map>
</property>
</properties>
</object>
</config>
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
index.php
<?php
// 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
var_dump($oFactory->getObject("myTable"));
// returns the myChair object
var_dump($oFactory->getObject("myChair"));
// returns the productManager object
var_dump($oFactory->getObject("productManager"));
$pm = $oFactory->getObject("productManager");
// returns the myChair object
var_dump($pm->products['ch01']);
?>
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.
 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!
 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.
Thanks.
 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
 chintan patel - 2011-04-13 07:28:14 - In reply to message 5 from Surekha Matte
hi,
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
|