Jump to content

[SOLVED] OOP methods relationship through inheritance


gevans

Recommended Posts

Hey guys, hopefully you can give me a quick hand here.

 

I have an initial class called 'page'. Some of it's methods include getting my html template files for output. I'm now using a class called 'adminPage' that extends 'page' and overwrites two of its methods.

 

The problem I'm having is calling the methods in 'page' that weren't overwritten in 'admiPage'.

 

I'll give some small snippets of the code bellow so you get an idea of what's going on...

 

You can ignore the 'controller' class, it's not causing any problem, bellow is 'page' class

 

<?php
class page extends controller {
    public $db;
    private $content = array();
    private $key = array();
    private $value = array();
    
    public function __construct() {
        parent::__construct();
        $this->db = new myMysqli(dbConfig::getHost(), dbConfig::getUser(), dbConfig::getPass(), dbConfig::getDb());
    }
    
    public function setDefaults($page, $key, $value = '') {
        if(is_array($key)) {
            for($i=0; $i<count($key); $i++) {
                $this->content[$page]['defaults'][$key[$i]] = $value[$i];
            }
        } else {
            $this->content[$page]['defaults'][$key] = $value;
        }
    }
    
    private function buildHtml($page) {
        if(isset($this->content[$page]['defaults'])) {
            foreach($this->content[$page]['defaults'] as $k => $v) {
                $this->key[$page][] = "{".$k."}";
                $this->value[$page][] = $v;
            }
        }
        $this->content[$page] = file_get_contents("../html/{$page}.html");
        $this->content[$page] = str_replace($this->key[$page], $this->value[$page], $this->content[$page]);
    }


............code missing

 

Next 'adminPage' class

 

<?php
class adminPage extends page {
    
    private function buildHtml($page) {
        if(isset($this->content[$page]['defaults'])) {
            foreach($this->content[$page]['defaults'] as $k => $v) {
                $this->key[$page][] = "{".$k."}";
                $this->value[$page][] = $v;
            }
        }
        $this->content[$page] = file_get_contents("../html/admin/{$page}.html");
        $this->content[$page] = str_replace($this->key[$page], $this->value[$page], $this->content[$page]);
    }
    
    public function printPage($extra='') {
        $this->buildHtml('header');
        echo $this->content['header'];
        if(!empty($extra)) {
            $this->buildHtml($extra);
            echo $this->content[$extra];
        }
        $this->buildHtml('footer');
        echo $this->content['footer'];
    }
}

 

Finally the index.php that calls 'adminPage' and attempts to use methods from both 'adminPage' and the inherited 'page' class

 

<?php
function __autoload($class_name) {
    require_once '../libs/' . $class_name . '.php';
}

$page = new adminPage();
if(!$page->getConf('online'))
    die('offline, do me');




$header_keys = array('BASE_HREF', 'CSS');
$header_values = array($page->getConf('url'), '');
$page->setDefaults('header', $header_keys, $header_values);
$page->setDefaults('header', 'JS');

$body_keys = array('PAGE_BODY');
$body_values = array('this is my page');
$page->setDefaults('body', $body_keys, $body_values);


echo $page->printPage('body');

 

The method setDefaults is not executing, or throwing an error?

Well, a little update.

 

private $content = array();

 

Seems to be the problem. Setting that to public allows me access to it from the child class. But I don't want it to be public, so I'm thinking of using a method to retrieve the array, or re-declare it in the child class.

 

My heads telling me there's a better way than this, if anyone has anyideas just jot it down :)

 

Cheers

I haven't actually,

 

I have a feeling I would have the same problem and would end up re-writting a lot of code with an interface.

 

INTERFACE - would outline the moethods and variable to use

 

PAGE and ADMINPAGE classes - virtually identical apart from a slight adaption in adminPage.

 

Thank's for the idea and I may play with that later.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.