Hello hope that you can help.

Basically, I'm making a site that uses a header file and then for each page, include the header and set the title.. It's easy using procedural PHP but I want to do it using OO. Here is the problem:

Page.php (class)

<?php

    class Page {

        protected $title;
        protected $header_loc;
        protected $mast_desc;

        public function __construct($theTitle="", $theHeader="", $mast_head="")
        {


        }

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

        public function _getTitle()
        {
            return $this->title;
        }
    }
?>

Header.php

<?php
  include ('Page.php');
  $header = new Page();

?>
<html class="no-js not-ie" lang="en"><!--<![endif]-->
<head>
<title><?php echo $header->_getTitle; ?></title>

Index.php

<?php
   include ('Page.php');
   $index = new Page('Welcome to the site', '/headers/header1.php', 'foo bar');
?>

Anyone see where I'm going wrong?

Thanks =)

In header you create a new object Page, where the constructor has no parameters, thus the title will be empty.

I think your Page constructor should load the header file into a property, and replace a title marker with the real title.

Thanks for your reply..

Could you give me a tiny example of what you mean? I'm sorry to ask, but, it's confusing me

Your header:

<html class="no-js not-ie" lang="en">
<head>
<title><!--HEADERTITLE--></title>

Your constructor:

public function __construct($theTitle="", $theHeader="", $mast_head="")
{
  $this->header = str_replace('<!--HEADERTITLE-->', $theTitle, file_get_contents($theHeader));
}

Fixed it :D This is really confusing me.. Any idea why $pageHeading is returning null?

<?php
    
    DEFINE ('HEADER_PATH', 'includes/headers/'); 
    class Page {
        
    	protected $title;
    	protected $header_loc;
    	protected $pageHeading;
    	
    	protected $header_file;
    	
    	public function __construct($theTitle, $theHeader, $theMast)
    	{
    		$this->title = $this->_title($theTitle);
    		$this->header_loc = $this->_header($theHeader);
    		$this->pageHeading = $theMast;
    	}
    	
    	public function setPageHeading($theMast)
    	{
    	   return $theMast;
    	}
    	public function _header($theHeader)
    	{	
                var_dump($this->pageHeading); // returns NUL
    		if(empty($theHeader))
    		{
    		   echo 'Error: You have not provided a header location';
    		}
    		
    		$this->header_file = (string) HEADER_PATH . $theHeader . ".php";
    		try {
    		  $page_title = $this->title;
    		  include ($this->header_file);
    		}catch(CusomException $e){
    		  if(!file_exists($file))
    		  {
    		     echo ('The specified file location does not exist!' + $e);
    		  }
    		}
    	}
    	
    	public function _title($theTitle)
    	{
    		if(empty($theTitle))
    		{
    			echo 'Error: You have not provided a page title';	
    		}
    		
    		if($theTitle == "[DYNAMICALLY ASSIGN]")
    		{
    		  $this->title = $this->_dynamically();
    		}else{
    		  $this->title = $theTitle;
    		}
    		
    		return $theTitle;
    	}
    	
    	public function _dynamically()
    	{
    		$title = 'Dynamically Assigned';
    		$this->title = $title;
    	}
    	
    	
    	public function _getTitle()
    	{
    		return $this->title;
    	}
    }
?>

Do you set it somewhere ? setHeading() is not setting, only getting...

in the constructor:

public function __construct($theTitle, $theHeader, $theMast)
    	{
    		$this->title = $this->_title($theTitle);
    		$this->header_loc = $this->_header($theHeader);
    		$this->pageHeading = $theMast;
    	}

I've also tried to use a function to set it as well - doesn't work

Your function

public function setPageHeading($theMast)
    	{
    	   return $theMast;
    	}

Doesn't actually set $pageHeading, it only returns the value passed in. It should be:

public function setPageHeading($theMast)
    	{
    	   $this->pageHeading = $theMast;
    	}

That should stop the null problem as long as you are calling the function properly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.