0

I can't get this to work.

<?php


        function __autoload($classname){
            include 'inc/classes/' . $classname . '.class.php';
        }



__autoload("queries")

$travel = new queries();
echo $travel->getPar("price");

?>

And this is the inc/classes/queries.class.php file.

<?

 class queries {

        function getPar($par, $table='travel', $type='select') {

            $result = $db->query("
             $type *
             FROM $table
             WHERE
             $par LIKE
            ");
            while ($row = $result->fetch_assoc()) {

                return "
                 $row[$par]
                ";
            }

    }
}

?>

It returns "Class 'queries' not found". What's wrong with it?

EDIT:

Fatal error: Cannot redeclare __autoload() (previously declared in /index.php:5) in /index.php on line 5

What the hell? I can't redeclare a function that is already declared in its own line, why?

djpredator17
  • 103
  • 7

3 Answers3

2

Try so (without class autoload):

function __autoload($classname){
    include_once 'inc/classes/' . $classname . '.class.php';
}
$travel = new queries();

Also see this link

yAnTar
  • 4,269
  • 9
  • 47
  • 73
  • I did as you suggested but we are back to the "class queries not found" issue. – djpredator17 Jul 07 '12 at 13:10
  • I suggest function __autoload, but you use function load and make so in function __autoload - var_dump($classname) - maybe troubles with paths. – yAnTar Jul 07 '12 at 13:16
  • Fatal error: Cannot redeclare class queries in inc/classes/queries.class.php on line 4 That's the line where I declare it, for the first time (there's not a second time...) – djpredator17 Jul 07 '12 at 13:18
  • Don't use call __autoload("queries") and instead include use include_once – yAnTar Jul 07 '12 at 13:24
  • Even if I just include_once the file (correctly), it doesn't recognize the class. – djpredator17 Jul 07 '12 at 13:27
  • Did you remove line __autoload("queries") ? – yAnTar Jul 07 '12 at 13:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13551/discussion-between-yantar-and-djpredator17) – yAnTar Jul 07 '12 at 13:53
1

Instead of that dreadful abomination, you should learn how to utilize spl_autoload_register():

spl_autoload_register( function( $classname ){

    $filename = 'inc/classes/' . $classname . '.class.php';

    if ( !file_exists( $filename) ){
        throw new Exception("Could not load class '$classname'.". 
                            "File '$filename' was not found !");
    }

    require $filename;

});

And you should register the autoloader in your index.php or bootstrap.php file, and do it only once per loader (this ability lets you define multiple loaders, but that's used, when you have third party library, which has own autoloader .. like in case of SwiftMailer).

P.S. please learn to use prepared statements with MySQLi or PDO.

Update

Since you are just now learning OOP, here are few things, which you might find useful:

Lectures:

Books:

Simon H
  • 2,495
  • 4
  • 30
  • 38
tereško
  • 58,060
  • 25
  • 98
  • 150
  • I just started with OOP, prepared statements will come a bit later, but really thanks for the suggestion :) – djpredator17 Jul 07 '12 at 14:20
  • @djpredator17 , added few links that might help you – tereško Jul 07 '12 at 17:02
  • If I could vote up your post i would do it at least twice :) ty so much! – djpredator17 Jul 08 '12 at 00:02
  • One line i forgot to add: [a tutorial for using PDO](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). From what I have seem, most of PDO tutorials out there are quite dreadful and filled with mistakes/injectable code. – tereško Jul 08 '12 at 00:05
0

remove this line from you code __autoload("queries"), you don't need to call autoloader it will be called by it self whenever it encounters any undeclared class and use require_once instead of include and debug if you paths are correct or not.

Rupesh Patel
  • 3,015
  • 5
  • 28
  • 49