#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    27
    Rep Power
    0

    PHP class HELP! -- writing class to load module positions from xml config


    Been trying to write classes and extended classes to handle loading of templates and modules... this is the start, but it's not working... can someone kindly point me in the right direction? This is all new to me...

    Code:
    class coreObj 
    {
    
    // Core variables
    var $path;
    var $xmlFile;
    var $xmlSchema;
    var	$type;
    
    	function load_xml_config ($path, $xmlFile, $xmlSchema)
    	{
    
    		$dom = new DomDocument;
    		$configFile = $path.$xmlFile;
    		$configSchema = $path.DS.$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.");
    			}
    	}	
    }
    // Define core template class
    class template extends coreObj 
    {
    	$type = 'template';
    	
    	function getPositions($xmlTag) 
    	{
    		parent::load_xml_config()
    		{
    		$doc = $dom->documentElement;
    		
    		// get XML element
    		$positions = $doc->getElementsByTagName($xmlTag);
    		$position = $positions->childNodes;
    		
    		foreach ($position as $position) {
    			echo '<div id="'.$position->nodeValue.'">'.$position->nodeValue.'</div>', "\n";
    			}
    		}
    	}
    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    27
    Rep Power
    0
    Rewrote some of it -- let me re-post:

    core.php

    Code:
    class coreObj 
    {
    
    // Core variables
    var $path;
    var $xmlFile;
    var $xmlSchema;
    var	$type;
    
    	function load_xml_config ($path, $xmlFile, $xmlSchema)
    	{
    
    		$dom = new DomDocument;
    		$configFile = $path.$xmlFile;
    		$configSchema = $path.DS.$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.");
    			}
    	}	
    }
    // Define core template class
    class template extends coreObj 
    {	
    	function getPositions() 
    	{	
    		// get XML element
    		$doc = $dom->documentElement;
    		$positions = $doc->getElementsByTagName('positions');
    		$position = $positions->childNodes;
    		
    		foreach ($position as $position) { echo '<div id="'.$position->nodeValue.'">'.$position->nodeValue.'</div>', "\n"; }
    	}
    }
    This is from index.php:

    Code:
    // Load module positions
    $template = new template();
    $template->load_xml_config(PATH_CONFIG.DS,'templ_default.xml','templ_default.xsd');
    $template->getPositions('positions');
    The output is as follows:

    XML config loaded
    Notice: Undefined variable: dom in C:\Servers\Apache\HTTP\2.2.13\htdocs\php_5_2_10\mirthsathorn.com\temp_html\classes\core.php on line 87

    Notice: Trying to get property of non-object in C:\Servers\Apache\HTTP\2.2.13\htdocs\php_5_2_10\mirthsathorn.com\temp_html\classes\core.php on line 87

    Fatal error: Call to a member function getElementsByTagName() on a non-object in C:\Servers\Apache\HTTP\2.2.13\htdocs\php_5_2_10\mirthsathorn.com\temp_html\classes\core.php on line 88


    Thanks,
    Lucas
    Last edited by lucaslopatka; September 18th, 2009 at 08:51 AM. Reason: Second thoughts...
  4. #3
  5. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2008
    Location
    North Carolina
    Posts
    2,674
    Rep Power
    2675
    Without actually pasting this into my IDE to figure out the line numbers, I noticed a problem here right off the bat:
    PHP Code:
    class template extends coreObj 
    {    
        function 
    getPositions() 
        {
            
    // get XML element
            
    $doc $dom->documentElement
    In this method, you need to declare $dom as a class property, and use it in the method above.
    PHP Code:
        function load_xml_config ($path$xmlFile$xmlSchema)
        {

            
    $dom = new DomDocument;
            
    $configFile $path.$xmlFile;
            
    $configSchema $path.DS.$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;
        } 
    PHP Code:
        function getPositions() 
        {    
            
    // get XML element
            
    $doc $this->dom->documentElement
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    27
    Rep Power
    0
    The same applies for
    Code:
    $position = $positions->childNodes;
    too, right?
  8. #5
  9. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2008
    Location
    North Carolina
    Posts
    2,674
    Rep Power
    2675
    No, not that I can see.

    When you set a variable within a method, it is not automatically made available to other methods:
    PHP Code:
    class MyClass {
        public function 
    hello() {
            
    $hello 'Hi!';
        }
        public function 
    world() {
            echo 
    $hello// $hello doesnt exist in this method
        
    }

    To make it available to other methods, you need to use class properties:
    PHP Code:
    class MyClass {
        protected 
    $hello;
        public function 
    hello() {
            
    $this->hello 'Hi!';
        }
        public function 
    world() {
            echo 
    $this->hello// outputs 'Hi!'
        
    }

    Therefore, unless you need to make a variable available to other methods within the class, there is no need to declare it as a class property.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    27
    Rep Power
    0
    Replaced $positions->childNodes; with getChildNodes(), which cut out one of two errors.

    I asked previously because I have one last error -- Call to undefined method DOMNodeList::getChildNodes()...
  12. #7
  13. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2008
    Location
    North Carolina
    Posts
    2,674
    Rep Power
    2675
    That would be because DOMNodeList does not have a getChildNodes() method.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    27
    Rep Power
    0
    Alright... got that last bit taken care of.

    Code:
    <?
    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 getError($i = null, $toString = true )
    	{
    		// Find the error
    		if ( $i === null) {
    			// Default, return the last message
    			$error = end($this->_errors);
    		}
    		else
    		if ( ! array_key_exists($i, $this->_errors) ) {
    			// If $i has been specified but does not exist, return false
    			return false;
    		}
    		else {
    			$error	= $this->_errors[$i];
    		}
    
    		// Check if only the string is requested
    		if ( JError::isError($error) && $toString ) {
    			return $error->toString();
    		}
    
    		return $error;
    	}
    
    /*	function getErrors()
    	{
    		return $this->_errors;
    	}
    */
    
    	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++;
    		}
    	}				
    }

    Two questions:

    1) My class loader isn't working again: I can't make a call like...
    Code:
    $template = new template();
    if(($template->validate_xml_config ($selected, $schema)) == true)
    {
    $template->loadObjects();
    .. without getting a "Fatal error: Call to a member function getElementsByTagName() on a non-object"

    How do I fix this?

    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!

    Cheers,

    Lucas

IMN logo majestic logo threadwatch logo seochat tools logo