__autoload question

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8411
  • Loc: USA

Post 3+ Months Ago

Could the magic method __autoload(); be used in a class or does it has to be outside of a class?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

What reason would you have for putting it in a class?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8411
  • Loc: USA

Post 3+ Months Ago

Code: [ Select ]
<?php
class database {
 
    /////////////////////////////
    /* Setting Class Variables */
    /////////////////////////////
   
    var $back_tick = true;
    var $load_ext = '.php';
   
    /////////////////////
    /* Class Functions */
    /////////////////////
   
    private function __construct()
    {
        // Setting some configurable values
        $this->tick = ($this->back_tick == true) ? '`' : "'";
       
    }
   
    function __autoload($class)
    {
        include($class . $this->load_ext);
    }
 
    private function valid_sql($sql)
    {
        $valid_queries = $valid->arrays()->queries();
       
        foreach($sql as $key => $junk)
        {
            if(!in_array($key, $valid_queries))
            {
                return false;
                break;
            }
        }
        return true;
    }
}
?>
  1. <?php
  2. class database {
  3.  
  4.     /////////////////////////////
  5.     /* Setting Class Variables */
  6.     /////////////////////////////
  7.    
  8.     var $back_tick = true;
  9.     var $load_ext = '.php';
  10.    
  11.     /////////////////////
  12.     /* Class Functions */
  13.     /////////////////////
  14.    
  15.     private function __construct()
  16.     {
  17.         // Setting some configurable values
  18.         $this->tick = ($this->back_tick == true) ? '`' : "'";
  19.        
  20.     }
  21.    
  22.     function __autoload($class)
  23.     {
  24.         include($class . $this->load_ext);
  25.     }
  26.  
  27.     private function valid_sql($sql)
  28.     {
  29.         $valid_queries = $valid->arrays()->queries();
  30.        
  31.         foreach($sql as $key => $junk)
  32.         {
  33.             if(!in_array($key, $valid_queries))
  34.             {
  35.                 return false;
  36.                 break;
  37.             }
  38.         }
  39.         return true;
  40.     }
  41. }
  42. ?>

No reason... I just don't know how to use it :lol: I don't know why I just don't include it... I guess I just want to try the __autoload(); thing.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

The __autoload() function is used so that you don't need to include class files at the beginning of your scripts. It naturally makes sense for __autoload() to exist as a regular function so that a class isn't required to load classes.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8411
  • Loc: USA

Post 3+ Months Ago

So, the following would work just the same?
Code: [ Select ]
<?php
function __autoload($class)
{
    include($class . '.php');
}
 
class database {
 
    /////////////////////////////
    /* Setting Class Variables */
    /////////////////////////////
   
    var $back_tick = true;
   
    /////////////////////
    /* Class Functions */
    /////////////////////
   
    private function __construct()
    {
        // Setting some configurable values
        $this->tick = ($this->back_tick == true) ? '`' : "'";
       
    }
   
    private function valid_sql($sql)
    {
        $valid_queries = $valid->arrays()->queries();
       
        foreach($sql as $key => $junk)
        {
            if(!in_array($key, $valid_queries))
            {
                return false;
                break;
            }
        }
        return true;
    }
}
?>
  1. <?php
  2. function __autoload($class)
  3. {
  4.     include($class . '.php');
  5. }
  6.  
  7. class database {
  8.  
  9.     /////////////////////////////
  10.     /* Setting Class Variables */
  11.     /////////////////////////////
  12.    
  13.     var $back_tick = true;
  14.    
  15.     /////////////////////
  16.     /* Class Functions */
  17.     /////////////////////
  18.    
  19.     private function __construct()
  20.     {
  21.         // Setting some configurable values
  22.         $this->tick = ($this->back_tick == true) ? '`' : "'";
  23.        
  24.     }
  25.    
  26.     private function valid_sql($sql)
  27.     {
  28.         $valid_queries = $valid->arrays()->queries();
  29.        
  30.         foreach($sql as $key => $junk)
  31.         {
  32.             if(!in_array($key, $valid_queries))
  33.             {
  34.                 return false;
  35.                 break;
  36.             }
  37.         }
  38.         return true;
  39.     }
  40. }
  41. ?>
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Based on what you've provided, no. The variable $valid isn't defined anywhere.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8411
  • Loc: USA

Post 3+ Months Ago

So if I put $valid = new valid(); the __autoload() thing would try to include valid.php... right?

And what if I use 'valid::arrays::queries()' instead? Would that work or would it bring up an error?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Quick note, magic methods within class definitions should always be defined as public methods, never protected or private.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8411
  • Loc: USA

Post 3+ Months Ago

You are talking about the __construct? I read here about the singleton method... I'm trying to implement that :)

Is that the wrong way to do it?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Either define them as public, or not at all.

You're going to get an error either way when you try to instantiate it, an error about a missing constructor is going to be much less confusing than one about the constructor being inaccessible due to visibility.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8411
  • Loc: USA

Post 3+ Months Ago

Alright then... I'll fix that

Post Information

  • Total Posts in this topic: 11 posts
  • Users browsing this forum: No registered users and 52 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.