PHP Classes

File: examples/example_001.php

Recommend this page to a friend!
  Classes of Stefan Rottensteiner   ClassGenerator   examples/example_001.php   Download  
File: examples/example_001.php
Role: Example script
Content type: text/plain
Description: Example
Class: ClassGenerator
Generate classes from definitions in XML
Author: By
Last change:
Date: 16 years ago
Size: 2,685 bytes
 

Contents

Class file image Download
<?php

// Turn on all errors for debuggin purposes
error_reporting(E_ALL);

require_once(
'../library/Util/ClassGenerator.php');

/**
 * A singleton class: The Family
 */
Class Family {

   
/**
     * The one and only instance will be kept here
     * @var Family
     */
   
protected static $instance = null;

   
/**
     * Mother
     * Used by the generator!
     * @var Mother
     */
   
protected $Mother = null;

   
/**
     * Father
     * @var Mother
     */
   
protected $Father = null;
   
   
/**
     * Protected constructor
     */
   
protected function __construct(){
       
// How to create a family ?
   
}

   
/**
     * Return the one an onyl family
     * Used by the generator!
     * @return Family
     */
   
public static function & getInstance(){
        if (empty(
self::$instance))
           
self::$instance= new Family();
        return
self::$instance;
    }

   
/**
     * Set father with this public method.
     * Used by the generator!
     * @param Father $Father
     */
   
public function setFather($Father){
       
$this->Father = $Father;
    }
   
   
/**
     * Get "father" object
     * @return Father
     */
   
public function getFather(){
        return
$this->Father;
    }
   
   
/**
     * Get "mother" object
     * return Mother
     */
   
public function getMother(){
        return
$this->Mother;
    }
}

/**
 * Used by the generator to create the missing class "Child"
 */
Class HumanBeeing {

    protected
$name = '<DefaultValue>';
   
    protected
$children = array();

   
/**
      * Used by the generator!
      */
   
protected $inCome = 0.0;
   
   
/**
      * Used by the generator!
      */
   
protected $sex = '?';
   
   
/**
     * Set the name of a human beeing
     * Used by the generator!
     * @param string $name
     */
   
public function setName( $name){
       
$this->name = $name;
    }

   
/**
     * Return the name
     * @return string
     */
   
public function getName( $name){
        return
$this->name;
    }

   
/**
     * Add a new child to this human beeing
     * Used by the generator!
     */
   
public function addChild(Child $child){
       
$this->children[] = $child;
    }
}

Class
Mother extends HumanBeeing{
    protected
$sex = 'female';
}

Class
Father extends HumanBeeing{
    protected
$sex = 'male';
}

/**
 * Define the family as XML, where the mother brings in the children
 */

$XMLFamily = <<<XMLString
<?xml version="1.0" standalone="yes" ?>
<Family xmlns:cgen="http://www.example.com/namespaces#ClassGenerator" cgen:factMethod="getInstance" >
    <Mother name="Heidelinde" inCome="2501.0" >
        <Child cgen:extends="HumanBeeing" name="Anna" sex="female"/>
        <Child name="Lena" sex="female"/>
    </Mother>
    <Father name="Franz" inCome="2500.0"/>
</Family>
XMLString;

// Get generator
$Generator = new Util_ClassGenerator();
// Create family ..
$Family = $Generator->createFromXML($XMLFamily);
// Let's take a look ..
print_r ($Family);