unemployment Posted October 28, 2011 Share Posted October 28, 2011 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/249983-declare-variables-in-php-class/ Share on other sites More sharing options...
unemployment Posted October 28, 2011 Author Share Posted October 28, 2011 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/249983-declare-variables-in-php-class/#findComment-1283032 Share on other sites More sharing options...
KevinM1 Posted October 28, 2011 Share Posted October 28, 2011 What are your errors? Where, specifically (read: line numbers), are they appearing? How are you trying to use your class? Quote Link to comment https://forums.phpfreaks.com/topic/249983-declare-variables-in-php-class/#findComment-1283048 Share on other sites More sharing options...
unemployment Posted October 28, 2011 Author Share Posted October 28, 2011 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]: ''; Quote Link to comment https://forums.phpfreaks.com/topic/249983-declare-variables-in-php-class/#findComment-1283056 Share on other sites More sharing options...
KevinM1 Posted October 28, 2011 Share Posted October 28, 2011 And what's the error on line 21...? Quote Link to comment https://forums.phpfreaks.com/topic/249983-declare-variables-in-php-class/#findComment-1283078 Share on other sites More sharing options...
KevinM1 Posted October 28, 2011 Share Posted October 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249983-declare-variables-in-php-class/#findComment-1283081 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.