Jump to content

Declare Variables in PHP Class


unemployment

Recommended Posts

I keep running into a bunch of error when I  declare the page variable in my model.  What syntax do I need to use throughout the class?

 

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Header_Model extends CI_Model{

var $page = substr(end(explode(DIRECTORY_SEPARATOR, $_SERVER['PHP_SELF'])), 0, -4);

function get_page_name()
{
	$this->load->library('common');

	$page_names = $this->common->page_names();

	$title = (array_key_exists($page, $page_names) !== false) ? $page_names[$page]: '';

	if (array_key_exists($page, $page_names) !== false)
	{
		$title .= " | Jason Biondo";
	}

	return $title;
}

function get_js_page_file()
{
	if (file_exists("./assets/js/pages/${page}.js"))
	{
		$javascript = "<script type=\"text/javascript\" src=\"./js/pages/${page}.js\"></script>";
	}

	return $javascript;
}

}

Link to comment
https://forums.phpfreaks.com/topic/249983-declare-variables-in-php-class/
Share on other sites

Here is my full class.  Can anyone optimize this?  I'm still getting errors where I have my page variables and I  don't know why.

 

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Header_Model extends CI_Model{

var $page 			= '';
var $nav_names		= array();
var $banner_imgs 	        = array();

public function __construct(){
	$this -> page = substr(end(explode(DIRECTORY_SEPARATOR, $_SERVER['PHP_SELF'])), 0, -4);
	$this -> nav_names = array('Contact', 'About', 'Tools', 'Portfolio', 'Writing');
	$this -> banner_imgs = array('contact.jpg', 'about.jpg', 'tools.jpg', 'portfolio.jpg', 'articles.jpg');
}

function get_page_name()
{
	$this->load->library('common');

	$page_names = $this->common->page_names();

	$title = (array_key_exists($this->$page, $page_names) !== false) ? $page_names[$this->$page]: '';

	if (array_key_exists($this->$page, $page_names) !== false)
	{
		$title .= " | Jason";
	}

	return $title;
}

function get_gutter_value()
{
	$gutter_values 	= array('307', '230', '161', '84', '0');

	foreach($nav_names as $k => $name)
	{
		if($this->$page === strtolower($name))
		{
			$g_value = $gutter_values[$k];
		}
		else
		{
			$g_value = 0;
		}
	}

	return $g_value;
}

function get_js_page_file()
{
	$page = $this->$page;

	if (file_exists("./assets/js/pages/${page}.js"))
	{
		$javascript = "<script type=\"text/javascript\" src=\"./js/pages/${page}.js\"></script>";
	}

	return $javascript;
}
}

What are your errors?  Where, specifically (read: line numbers), are they appearing?  How are you trying to use your class?

 

Error is.. Fatal error: Cannot access empty property in /home/www-data/demo.jason.com/system/core/Model.php on line 50

 

I am trying to use my class to set up the header data I need.  Page title, navigation info, page name...

 

There is also an error on line 21

 

$title = (array_key_exists($this->$page, $page_names) !== false) ? $page_names[$this->$page]: '';

Also, in your get_js_page_file() method, try:

 

	function get_js_page_file()
{	
	if (file_exists("./assets/js/pages/{$this->page}.js"))
	{
		$javascript = "<script type=\"text/javascript\" src=\"./js/pages/{$this->page}.js\"></script>";
	}

	return $javascript;
}

 

Keep in mind, that if the file doesn't exist, your method will still attempt to return a then null $javascript variable since the return statement is outside of your conditional.

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.