Page 1 of 1

Extended class disables parent function?

Posted: Wed Jan 26, 2011 7:23 pm
by Domsore
Okay so I spend half an hour trying to get to the root of the problem... I finally found it, but do not know how to fix it. :oops:

So here is my parent:

Code: Select all

<?php
/**
 * Description of template
 *
 * @author Dominic Sore
 */
class template {
    
    public $templateName;

    public function setTemplate($lTemplate){
	$this->templateName = $lTemplate;
    }

    public function loadTemplate(){
	echo '<link rel="stylesheet" type="text/css" href="template/'.$this->templateName.'/css/'.$this->templateName.'.css" />';
    }


}
?>
and here is my Extended class:

Code: Select all

<?php
/**
 * Description of headerProperties
 *
 * @author Dominic Sore
 */
class headerProperties extends template {

    public $title;

    public function setTitle($pTitle){
	$this->title = $pTitle;
    }

    public function insTitle(){
	echo '<title>'.$this->title.'</title>';
    }

    public function insMeta(){
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
    }

    public function createHeader(){
	echo '<head>';
	$this->insTitle();
	$this->insMeta();
	parent::loadTemplate();
	echo '</head>';
    }
}
?>
and here is where I execute it all:

Code: Select all

<?php
/**
 * Description of Global_Conf
 *
 * @author Dominic Sore
 */
class globalConf {

    public function  __construct() {
	
	/* Require relevant includes */

        require_once 'database.php';
	require_once 'login.php';
	require_once 'template.php';
	require_once 'headerProperties.php';


	/* Load classes */

        $dbConnect = new database();
	$logFunc = new login();
	$tempProperties = new template();
	$headProperties = new headerProperties();

	/* Database Functions */

	$dbConnect->parseIni();
	$dbConnect->dbConnect();

	/* Template Properties */

	$tempProperties->setTemplate('default');

	/* Header Properties */

	$headProperties->setTitle('Testing');
	$headProperties->createHeader();

    }
}
?>
So as you can see I execute the setTemplate('default'); first. This should then set the public $templateName to default, but it doesnt and I can not figure out why. I included and echo in the loadTemplate function to make sure it wasn't that and that seemed to work fine. Any ideas?

Cheers,

Dom.

Re: Extended class disables parent function?

Posted: Wed Jan 26, 2011 9:08 pm
by Jonah Bron
Even though their classes are connected through inheritance, those are two different instances. You're calling setTemplate() on $tempProperties, while you call createHeader() on $headProperties. Two different objects, with independent properties.

If you echo $tempProperties->templateName, it would output "default". If you echo $headProperties->templateName, you don't. That demonstrates that it is in fact setting the variable, but the two objects are not connected in the way you thought.

To fix it, remove $tempProperties, and call setTemplate() on $headProperties.

Code: Select all

<?php
/**
 * Description of Global_Conf
 *
 * @author Dominic Sore
 */
class globalConf {

    public function  __construct() {
       
        /* Require relevant includes */

        require_once 'database.php';
        require_once 'login.php';
        require_once 'template.php';
        require_once 'headerProperties.php';


        /* Load classes */

        $dbConnect = new database();
        $logFunc = new login();
        $headProperties = new headerProperties();

        /* Database Functions */

        $dbConnect->parseIni();
        $dbConnect->dbConnect();

        /* Template Properties */

        $headProperties->setTemplate('default');

        /* Header Properties */

        $headProperties->setTitle('Testing');
        $headProperties->createHeader();

    }
}
?>
You should read up on Object Oriented Programming and Classes/Objects.

http://www.killerphp.com/tutorials/obje ... page-1.php

Also, in headerProperties(), you need to change this function:

Code: Select all

public function createHeader(){
        echo '<head>';
        $this->insTitle();
        $this->insMeta();
        parent::loadTemplate(); // this is wrong
        echo '</head>';
    }
That's wrong. The child class receives all methods from it's parent (except ones marked private). Access them directly with $this->

Code: Select all

public function createHeader(){
        echo '<head>';
        $this->insTitle();
        $this->insMeta();
        $this->loadTemplate();
        echo '</head>';
    }

Re: Extended class disables parent function?

Posted: Thu Jan 27, 2011 12:07 am
by Domsore
Cheers for the tips... it worked a treat.

Thanks,

Dom