Initializing class, sub class & methods -- HELP!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lucaslopatka
Forum Newbie
Posts: 4
Joined: Mon Jun 22, 2009 11:53 pm

Initializing class, sub class & methods -- HELP!

Post by lucaslopatka »

Two questions:

1) My class loader isn't working: I can't the call $template->loadObjects() without getting a "Fatal error: Call to a member function getElementsByTagName() on a non-object"

How do I fix this?

Code: Select all

 
$selected = PATH_THEMES.DS.'default'.DS.'index2.php';
$schema = PATH_CONFIG.DS.'templ_default.xsd';
 
$template = new template();
if(($template->validate_xml_config ($selected, $schema)) == true)
{
$template->loadObjects();
// Start output buffer
ob_start();
include ($selected);
ob_end_flush();
}
 
2) Right now, I have coreObj as the parent class, and template, module, plugin, etc. as child classes. In the 'switch' section of loadObject(), how can I initialize a new instance of an object, per case (module/component/etc.)?

I really appreciate the help! Couldn't learn all of this without it!

Here's my code:

Code: Select all

 
<?
class coreObj 
{
 
// Core variables
var $path;
var $xmlFile;
var $xmlSchema;
var $type;
var $name;
var $id;
var $style;
 
var $_errors = array();
 
     function coreObj()
    {
        $args = func_get_args();
        call_user_func_array(array(&$this, '__construct'), $args);
    }
    
    function __construct() {}
     
     public function validate_xml_config ($xmlFile, $xmlSchema)
    {
 
        $dom = new DomDocument;
        $configFile = $xmlFile;
        $configSchema = $xmlSchema;
        // Load the xml document in the DOMDocument object
        $dom = DOMDocument::load($configFile);
        // Validate the XML file against the schema
        if (($dom->schemaValidate($configSchema)) == false)
            {
            echo '<pre>';
            throw new Exception ("$configFile is an invalid configuration file");
            echo '</pre>';
            //echo "XML config loaded";
            } 
        return true;
    }
    
         public function load_xml_config ($xmlFile, $xmlSchema)
    {
 
        $dom = new DomDocument;
        $configFile = $xmlFile;
        $configSchema = $xmlSchema;
        // Load the xml document in the DOMDocument object
        $dom = DOMDocument::load($configFile);
        // Validate the XML file against the schema
        if ($dom->schemaValidate($configSchema))
            {
            null;
            //echo "XML config loaded";
            } 
            else 
            {
            die ("$configFile is an invalid config file. $type cannot be loaded.");
            }
            $this->dom = $dom; 
    }
    
    function get($property, $default=null)
    {
        if(isset($this->$property)) {
            return $this->$property;
        }
        return $default;
    }
 
    function getProperties( $public = true )
    {
        $vars  = get_object_vars($this);
 
        if($public)
        {
            foreach ($vars as $key => $value)
            {
                if ('_' == substr($key, 0, 1)) {
                    unset($vars[$key]);
                }
            }
        }
 
        return $vars;
    }
 
    function set( $property, $value = null )
    {
        $previous = isset($this->$property) ? $this->$property : null;
        $this->$property = $value;
        return $previous;
    }
 
    function setProperties( $properties )
    {
        $properties = (array) $properties; //cast to an array
 
        if (is_array($properties))
        {
            foreach ($properties as $k => $v) {
                $this->$k = $v;
            }
 
            return true;
        }
 
        return false;
    }
    function setError($error)
    {
        array_push($this->_errors, $error);
    }
    function toString()
    {
        return get_class($this);
    }
    function getPublicProperties()
    {
        return $this->getProperties();
    }
    
        
}
 
// Define core template class
class template extends coreObj 
{
    protected $positions;   
    public function getPositions() 
    {    
        // get XML element
 
        $modPositions = array();
        $doc = $this->dom->documentElement;  
        $positions = $doc->getElementsByTagName('position');
        
        $i = 0;
        foreach ($positions as $position) {
            $modPositions[] = $position->nodeValue;
            $this->modPositions = $modPositions;
            //echo $modPositions[$i], "\n";
            //$i = $i + 1;
        }
    }
    public function createPositions()
    {
        foreach ($this->modPositions as $position) {
            echo '<div id="'.$position.'">This is the '.$position.' module position.</div>', "\n";
        }
    }
    public function loadObjects()
    {   
        $doc = $this->dom->documentElement;
        $objects = $doc->getElementsByTagName('xinclude');
        $i = 0;
        foreach ($objects as $object) 
        {
            
            $type = $object->getAttribute('type');
            switch ($type)
            {
                case 'head':
                    break;
                
                case 'component':
                    break;
                
                case 'module':
                    $name = $object->getAttribute('name');
                    $style = $object->getAttribute('style');
                    $id = $object->getAttribute('id');
                    echo $type.', '.$name.', '.$style.', '.$id, "\n";               
                    break;
                
                case 'wrapper':
                    break;
                
                case 'message':
                    break;
            }
            $i++;
        }
    }               
}
 
Cheers,

Lucas
Post Reply