Updated template class for CMS I'm not doing too well in school, so to pass my final year, and get my degree I need to do an off-school project. The teacher chose to give me the assignment of an A grade CMS, since I've always had a thing with PHP.

Now I've made a new template class for in this particular CMS, but I'm not completely sure about it, so I came here for your help. Please review my code.


Have I used things in a correct way?
Have I used things in a efficient way?
Is there a better way to achieve the same result?




class template
{

    function __construct( $db, $functions )
    {
        $this-&gt;db = $db;
        $this-&gt;functions = $functions;
    }

    public function pageExist( $id )
    {
        $query = $this-&gt;db-&gt;connection-&gt;prepare( 'SELECT id FROM ' . $this-&gt;functions-&gt;prefix( 'pages' ) . ' where id = ?' );
        $query-&gt;bind_param( 'i', $id );
        $query-&gt;execute();
        $query-&gt;store_result();
        if ( $query-&gt;num_rows &gt; 0 )
        {
            return true;
        } 
            else
        {
            return false;
        }
        $query-&gt;close();
    }

    public function fetchPage( $id )
    {
        $query = $this-&gt;db-&gt;connection-&gt;prepare( 'SELECT * FROM ' . $this-&gt;functions-&gt;prefix( 'pages' ) . ' WHERE id = ?' );
        $query-&gt;bind_param( 'i', $id );
        $query-&gt;execute();
        $result = $query-&gt;get_result();
        while ( $row = $result-&gt;fetch_assoc() )
        {
            $this-&gt;setTemplateData( 'title', $num_rows[ 'title' ] );

        }
        $query-&gt;close();
    }

    public function getTheme()
    {
        $theme = 'theme';
        $query = $this-&gt;db-&gt;connection-&gt;prepare( 'SELECT value FROM ' . $this-&gt;functions-&gt;prefix( 'settings' ) . ' WHERE name = ?' );
        $query-&gt;bind_param( 's', $theme );
        $query-&gt;execute();
        if ( ! $query )
        {
            throw new Exception( $query-&gt;error(), 1 );
        }
        $query-&gt;bind_result( $result );
        while ( $query-&gt;fetch() )
        {
            return $result;
        }

    }

    public function loadPage()
    {
        $path = './_templates/' . $this-&gt;getTheme() . '/';
            $this-&gt;content = file_get_contents( $path . 'header.tpl');
            $this-&gt;content .= file_get_contents( $path . 'middle.tpl');
            $this-&gt;content .= file_get_contents( $path . 'footer.tpl');
    }

    public function createParameter( $key, $value )
    {
        $this-&gt;parameter[ $key ] = $value;
    }

    public function langParameters( $key, $value )
    {
        $this-&gt;langParameters[ $key ] = $value;
    }

    public function setTemplateData( $key, $value )
    {
        $this-&gt;templateData[ $key ] = $value;
    }

    public function parseParameters()
    {
            foreach ($this-&gt;parameter as $key =&gt; $value)
            {
                $this-&gt;content = str_replace( '{' . $key . '}', $value, $this-&gt;content);
            }

            foreach ($this-&gt;langParameters as $key =&gt; $value) 
            {
                $this-&gt;content = str_replace( '{' . $key . '}', $value, $this-&gt;content);
            }

            foreach ($this-&gt;templateData as $key =&gt; $value) 
            {
                $this-&gt;content = str_replace( '{' . $key . '}', $value, $this-&gt;content);
            }


        echo $this-&gt;content;
    }

    public function handlePageLoad( $id )
    {
        if ( $this-&gt;pageExist( $id ) )
        {
            $this-&gt;fetchPage( $id );
            $this-&gt;loadPage();
            $this-&gt;parseParameters();
            return true;
        }
        else
        {
            $this-&gt;fetchPage( 404 );
            $this-&gt;loadPage();
            $this-&gt;parseParameters();
            //header('Location: index.php?page=404');
            echo '! &lt;br /&gt; He\'s a true genius.';
        }
    }


}
The class name, why template? Template is much better and follows the naming convention in all sane languages (oook, i know it's PHP :()

Where are $db and $functions? You missed to declare them in the class

class template
{
   private $db;
   private $functions = [];


Your

$query = $this-&gt;db-&gt;connection-&gt;prepare( 'SELECT id FROM ' . $this-&gt;functions-&gt;prefix( 'pages' ) . ' where id = ?' );


You use prepared statement for where id = ? but now for $this-&gt;functions-&gt;prefix( 'pages' ) it makes the prepared statement useless.

The same for fetchPage, getTheme.

As user45891 said, your $query-&gt;close() will never be executed due to the return. You can improve this with:

$query-&gt;close();
return $query-&gt;numRows &gt; 0;


This block

if ( ! $query )
{
    throw new Exception( $query-&gt;error(), 1 );
}


Great, i love Exceptions but what about a more specific exception? When i write PHP code i check this list to know which exceptions php have. What about UnexpectedValueException or LogicException. LogicException should be used when the error in your code, if $query is false it means you DB is OFF or your question is wrong so the problem should be in your code.

Anyway, your check should go before you use $query! because it will fails with

 $query-&gt;bind_param( 's', $theme );




while ( $query-&gt;fetch() )
{
   return $result;
}


You need only the first value? Well, what about 

$query-&gt;fetch();
return $result;


?

Anyway, you don't close $query in every function.

Your handlePageLoad don't return everytime, only if it joins $this-&gt;pageExist( $id ) path. Maybe you want to add return false;?

You could improve the this code with this:

public function handlePageLoad( $id )
{
    if ( $this-&gt;pageExist( $id ) )
    {
        $this-&gt;fetchPage( $id );
        $this-&gt;loadPage();
        $this-&gt;parseParameters();
        return true;
    }

    $this-&gt;fetchPage( 404 );
    $this-&gt;loadPage();
    $this-&gt;parseParameters();
    //header('Location: index.php?page=404');
    echo '! &lt;br /&gt; He\'s a true genius.';
    return false;
}


The indentation of parseParameters is not coerent with the other code (and ugly :()

As i said above, you missed to declare fields in your class. Do it! You will need to remember what fields you use and what fields your class have (and your IDE will help you more!)

How you handle the case where someone uses the same name for lang and template data?

Your parseParameters feels to be very slow with many arguments... what about RegEx?

Why you use three different arrays parameter, langParameters and templateData (except this which is filled by another method) ? They do the same thing? What about only one array?

You need it separate?



I don't use PHP everyday (only when i need to) so i'm not sure, so don't take it serious:

In getTheme you used $theme = 'theme'; because bind_param wants references ok, but what about &amp;"theme"? I'm not sure, but you could try

Anyway, good luck!