Jump to content

[SOLVED] Autoload function not working


lordzardeck

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

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

Link to comment
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.

Link to comment
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()

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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