lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 how do I get the set_page() function to set the variable so get_page() uses it? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 21, 2009 Share Posted February 21, 2009 You might want to read up on object oriented programming in php5, many sources go into great depth on the subject. Or if you want to just go through the documentation real quick, http://us.php.net/zend-engine-2.php gives a good no-BS rundown of php5's OOP. Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 i have apress's pro php and beginning php and mysql. But I can't seem to get it to work. Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 the problem i'm having now is it won't declare the varible as private. This is the error: Parse error: syntax error, unexpected T_PRIVATE in /homepages/36/d134836550/htdocs/test/admin/classes/pagehandler.php on line 5 This is the code: <?php require_once("./classes/autoload.php"); private $page_load; //start page render class class PageHandler{ function page_build(){ PageHandler::add_header(); PageHandler::load_styles(); PageHandler::end_header(); PageHandler::add_body(); PageHandler::add_menu(); PageHandler::add_content(); PageHandler::end_page(); } function add_header(){ print "<html>"; print "\n<head>\n<title>SAC Online Catalog</title>"; } function end_header(){ print "\n</head>"; } function add_body(){ print "\n<body>"; } function end_page(){ print "\n</body>\n</html>"; } function add_menu(){ if(!$menu){ $menu = new Menu(); }else{ $menu->menu_build(); } } function load_styles(){ $css_dir = opendir('./css'); while($file = readdir($css_dir)){ if($file != "." AND $file != ".."){ print "\n<link rel='stylesheet' type='text/css' href='$file' media='all' />"; } } closedir($css_dir); } function set_page($function_page_load){ $this->$page_load = $function_page_load; } function get_page(){ return $this->$page_load; } function add_content(){ $page = PageHandler::get_page(); $content = new $page(); $content->build_page(); } } ?> Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 21, 2009 Share Posted February 21, 2009 You may want to run through the chapter on OOP then, because there are a couple of issues with your class. But to fix this particular issue... <?php public static function set_page($function_page_load){ $this->page_load = $function_page_load; } public static function get_page(){ return $this->page_load; } That's not to say there aren't design issues with your class, this is merely how to fix it syntactically. As far as the private member, you need to put it inside of the class... <?php class PageHandler { private $page_load; // ... } Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 oh.... I feel stupid. Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 great... Now I get this: Fatal error: Using $this when not in object context in /homepages/36/d134836550/htdocs/test/admin/classes/pagehandler.php on line 79 Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 21, 2009 Share Posted February 21, 2009 Oh, well I improperly declared it static then used $this-> you'd need to use self::, sorry about that... In my defense I never use purely static classes. Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 I don't understand static that much. That's why I never use them. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 21, 2009 Share Posted February 21, 2009 You'd do it similar to this... <?php class StaticTest { private static $variable; public static function setVar($var) { self::$variable = $var; } public static function getVar() { return self::$variable; } } StaticTest::setVar('test'); echo StaticTest::getVar(); Static is nice for some things like singletons, but using purely static classes are like programming procedurally with objects... most of the time it shows a design flaw. Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 Now I get this (no, I mean self:: ): Fatal error: Access to undeclared static property: PageHandler::$page_load in /homepages/36/d134836550/htdocs/test/admin/classes/pagehandler.php on line 79 Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 never mind. It works now! Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 can you have a static variable? if so, how do you declare it as a variable? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 21, 2009 Share Posted February 21, 2009 What do you mean? Like private static $variable; ? Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 actually, I found that out. My problem is it is an array. I don't know if I declared it right, or if I'm not using it right or what. Here is my script: <?php class Menu{ private static $pages = array(); function _autoload($class){ require_once($class.".php"); } function menu_build(){ Menu::load_pages(); print_r(self::$pages); print "<div class='menu'>\n<ul>"; foreach(self::$pages as $menu_item){ print "\n<li><a href='index.php?$menu_item'>$menu_item</a></li>"; } print "\n</ul>\n</div>"; } //loads all pages and places them in the array pages[] function load_pages(){ $page_dir = opendir('./classes'); while($file = readdir($page_dir)){ if($file != "." AND $file != ".."){ $file_type = substr($file, 0, 0); if($file_type == 'P'){ self::$pages = strstr($file, '.', true); } } } closedir($page_dir); return self::$pages; } } ?> The print_r() is used for me to know if there is anything in it. All I get in return is Array() Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 22, 2009 Author Share Posted February 22, 2009 for some reason I'm not getting errors. If I try to add something to the array outside the function, it works. Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 22, 2009 Share Posted February 22, 2009 Declare these functions as static. Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 22, 2009 Author Share Posted February 22, 2009 actually, I decided to go another route with adding menus. It makes it easier to add menus as the pages increase. My problem now is how do I check if a filename has an extension. I don't want to get folders used when I scan the directory. Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 22, 2009 Share Posted February 22, 2009 is_file Quote Link to comment Share on other sites More sharing options...
lordzardeck Posted February 26, 2009 Author Share Posted February 26, 2009 sweet! thanks! Quote Link to comment 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.