0

I am writing a small framework for myself to help build my site, my current example is an object function to start an HTML page, it will be ran like this:

$page->head(array(
    'doctype' => 'html4',
    'description' => 'My page'
    //etc
));

I wish to have a default array (as defined in head()) to have default values in case they are not defined. It may look like this:

$defaults = array('doctype' => 'html5', 'author' => 'default', ...);

What would be the best way to replace each default element with one defined in the head function's arguments when called?

I found something maybe like this, being unsure of what it exactly does but it has the right idea http://www.php.net/manual/en/function.array-replace.php#94458

2 Answers 2

3

It sounds like you may be able to do this with array_merge, like this:

$page->head(array_merge($defaults,array(
  'doctype' => 'html4',
  'description' => 'My Page'
  // etc
)));

This will overwrite any value from $defaults with the new value, while preserving any key that does not have a new value.

1
  • Thank you for answering the original question, I understand more about this. Commented Jan 3, 2011 at 7:08
0

Is there a reason that you're not using classes for this? It sounds exactly like a problem that would benefit from OOP. For example:

class PageHeader
{
    private $m_sDoctype;
    private $m_sDescription;
    // other member vars, such as an array of css files

    function __construct($doctype, $desc)
    {
        // set your default values
        $this->m_sDoctype = _isset($doctype) && $doctype != null ? $doctype : 'html4';
        $this->m_sDescription = _isset($desc) && $desc != null ? $desc : 'My Page';
    }

    function render()
    {
        // dump out html however you see fit
    }
}

class BasePage
{
    private $m_oPageHeader;
    // other member vars

    function __construct($doctype, $desc)
    {
         $m_oPageHeader = new PageHeader($doctype, $desc);
    }

    function render()
    {
        $this->m_oPageHeader->render();
    }
}

index.php (or whatever your homepage is):

var $page = new BasePage(null, 'Home Page');
$page->render();

A couple notes:

  1. This is a very simplistic example that I haven't tested
  2. I know that this isn't really what you were looking for (as you were looking for a solution to your array problem), but I were developing a small framework for myself, this is definitely the way I'd go about doing it as there are clear benefits to using OOP in any environment.

Edit: I forgot to mention that, by using constructors with default values, you get the desired functionality of having the defaults, but also having the ability to overwrite them.

1
  • You are right, my semi-OO approach was lacking practical implementation, I will base it off classes and extending classes now. Thank you. Commented Jan 3, 2011 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.