1

I'm trying to use require_once to call Mail.php but when I create a new instance for that class and start using it I get a message that says

Fatal error: Class 'Mail' not found in D:\xampp\htdocs\proj\Mailing\mymail.php on line xx

The resumed code goes like this

<?php
     require_once 'Autoload.php';

     $smtp = Mail::factory('smtp',
          array ('host' => $host,
          'port' => $port,
          'auth' => true,
          'username' => $username,
          'password' => $password));
?>

or also

<?php
     require_once 'Mail.php';

     $mail = new Mail();
?>

Why do I get that message? I already set the include path to ".;D:\xampp\php\pear"

I edited my script and required the autoload script, which works just fine, cause it prints no error, but I still get the same message like Mail doesn't work (I edited the script above), autoload is in the same directory where my main script is.

I made a new file and called it with the full path, and then the output was

Warning: require_once(D:\xampp\php\PEAR) [function.require-once]: failed to open stream: Permission denied in D:\xampp\htdocs\proj\Mailing\pMail.php on line 2
hakre
  • 193,403
  • 52
  • 435
  • 836
Damian Rojas
  • 35
  • 1
  • 9
  • Are you sure that Mail.php is kept in the folder D:\xampp\htdocs\proj\Mailing\mymail.php ??? – SHAKIR SHABBIR Jun 26 '12 at 13:48
  • Where do you define the `Mail` class? Since the `require_once` call is obviously succeeding (otherwise you would get an error about that instead), the problem is that the Mail class is not defined in the included file. – Emil Vikström Jun 26 '12 at 13:49
  • Mail is a clas from PEAR's Mail.php package, so it is kept in "D:\xampp\php\pear", so it's location is "D:\xampp\php\pear\Mail.php", it's not in the project folder, but the iclude path points to that directory. – Damian Rojas Jun 26 '12 at 16:05

1 Answers1

0

When you are loading a class you don't need to require your php file if you use autoloading.

Example:

Class Autoload {
   private $rootDirectory;
   private $paths = array(
       "system/core",
       "system/obj",
       "system/data"
   );

   public function __construct() {
       $this->RootDirectory = dirname(getcwd()); // Whatever is your root
       $this->activateAutoload("autoloadClassesFromSystemFolder");
   }

   private function autoloadClassesFromSystemFolder($i) {
       foreach ($this->paths as $path) {
           // Please keep in mind that a class name has to be unique. 
           if (is_file($this->RootDirectory .'/'. $path .'/'. $i . ".php")) {
               include $this->RootDirectory .'/'. $path .'/'. $i . ".php";
               break;
           }
       }
   }

   public function activateAutoload($functionName) {
       spl_autoload_register(array($this, $functionName));
   }
}

The only thing you need to do to load this in php is:

require "path/to/autoload.php";
$autoload = new Autoload();

After that you can use as many classes as you want without having to worry about class file includements.

$mail = new Mail();

More info: http://nl.php.net/autoload

Sem
  • 4,477
  • 4
  • 33
  • 52
  • so @Sem where it says dirname(getcwd()); i would change getcwd() to my own PEAR directory? like, dirname(getcwd('D:\xampp\php\pear')); is that so? – Damian Rojas Jun 26 '12 at 16:09
  • I still get the same error, like the file is not included, do you need the full script? It's just that I think that's against Stack Overflow idea about not being a too specific problem. – Damian Rojas Jun 26 '12 at 16:21
  • Hmm, sorry for such a late answer. Like I said in the comment you have to use your root directory. – Sem Jun 27 '12 at 08:48
  • it should be like $this->RootDirectory = "D:\xampp\php\pear". – Sem Jul 02 '12 at 18:05