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? Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768003 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. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768010 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. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768013 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(); } } ?> Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768017 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; // ... } Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768020 Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 oh.... I feel stupid. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768023 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 Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768027 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. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768028 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. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768029 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. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768030 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 Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768031 Share on other sites More sharing options...
lordzardeck Posted February 21, 2009 Author Share Posted February 21, 2009 never mind. It works now! Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768032 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? Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768035 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; ? Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768036 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() Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768040 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. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768372 Share on other sites More sharing options...
Mchl Posted February 22, 2009 Share Posted February 22, 2009 Declare these functions as static. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768378 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. Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768385 Share on other sites More sharing options...
Mchl Posted February 22, 2009 Share Posted February 22, 2009 is_file Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-768416 Share on other sites More sharing options...
lordzardeck Posted February 26, 2009 Author Share Posted February 26, 2009 sweet! thanks! Link to comment https://forums.phpfreaks.com/topic/146279-solved-autoload-function-not-working/page/2/#findComment-772003 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.