Jump to content

Archived

This topic is now archived and is closed to further replies.

lordzardeck

[SOLVED] Autoload function not working

Recommended Posts

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();
          
      }
  }
?>

Share this post


Link to post
Share on other sites

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;
    // ...
}

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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()

Share this post


Link to post
Share on other sites

Declare these functions as static.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

×
×
  • 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.