Class not found PHP OOP -
i can't work.
<?php function __autoload($classname){ include 'inc/classes/' . $classname . '.class.php'; } __autoload("queries") $travel = new queries(); echo $travel->getpar("price"); ?>
and inc/classes/queries.class.php file.
<? class queries { function getpar($par, $table='travel', $type='select') { $result = $db->query(" $type * $table $par "); while ($row = $result->fetch_assoc()) { return " $row[$par] "; } } } ?>
it returns "class 'queries' not found". what's wrong it?
edit:
fatal error: cannot redeclare __autoload() (previously declared in /index.php:5) in /index.php on line 5
what hell? can't redeclare function declared in own line, why?
instead of dreadful abomination, should learn how 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' not found !"); } require $filename; });
and should register autoloader in index.php
or bootstrap.php
file, , once per loader (this ability lets define multiple loaders, that's used, when have third party library, has own autoloader .. in case of swiftmailer).
p.s. please learn use prepared statements mysqli or pdo.
update
since learning oop, here few things, might find useful:
lectures:
- advanced oo patterns
- inheritance, polymorphism, & testing
- recognizing smelly code
- global state , singletons
- don't things!
books:
Comments
Post a Comment