0

I have been checking for answers but with no success. I'm not using composer autoload function.

I'm trying to test some classes, Collections and traits in a index.php. The problem is when i'm including a namespace, I'have created the _autoload function (No composer) to load a class when needed. If I execute php index.php i'm getting the class not found error.

I have tried the use command, but with no success as well.

It solves when I include each class one by one like this:

include_once __DIR__ . '/Collection.php';
include_once __DIR__ . '/User.php';
include_once __DIR__ . '/Tweet.php';
include_once __DIR__ . '/UserCollection.php';
include_once __DIR__ . '/TweetCollection.php';

Thats my index.php:

<?php

namespace phpexercises\entregableT3;

error_reporting(E_ALL);
ini_set( "display_errors", "on" );
/*
include_once __DIR__ . '/Collection.php';
include_once __DIR__ . '/User.php';
include_once __DIR__ . '/Tweet.php';
include_once __DIR__ . '/UserCollection.php';
include_once __DIR__ . '/TweetCollection.php';
*/
use phpexercises\entregableT3\User;

function __autoload($classname){
   require __DIR__ . "/" . $classname . ".php";

}

//UserCollection pruebas

$alex = new User();
$jumbo = new User();

$users = new UserCollection();
$alex->phone = "682383";
$alex->email = "alex@example.com";
$alex->city = "BCN";
$alex->gender = "Male";


$jumbo->phone = "54534535";
$jumbo->email = "jumbo@example.com";
$jumbo->city = "elche";
$jumbo->gender = "Male";

$users->add($alex);
$users->add($jumbo);



print_r($users->findByEmail("alex@example.com"));
print_r($users->findByGender("Female"));

//Tweet Collection pruebas

$tweet = new Tweet();
$tweet->email  = "alex@example.com";
$tweet->text = "This is a tweet";
$tweet->date = date("D:M:Y");

$tweet1 = new Tweet();
$tweet1->email  = "alex@example.com";
$tweet1->text = "This is a tweet";
$tweet1->date = date("D:M:Y");

When I uncomment the requires, everything works fine, but it is not the way to load all classes...Any idea for this behaviour? How should I load the classes at same time? Thanks

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
Albeis
  • 1,544
  • 2
  • 17
  • 30
  • I see you have a `use` statement for the `User` class but not for the `UserCollection` class. Are they in the same namespace as your index.php? To see what PHP is trying to load them as when `__autoload()` is called, add an `echo $classname;`. You may need to do additional string manipulation to the variable so it matches your path name. – Michael Berkowski Dec 28 '16 at 12:26
  • http://stackoverflow.com/questions/10965454/how-does-the-keyword-use-work-in-php-and-can-i-import-classes-with-it – Gogol Dec 28 '16 at 12:27
  • The value of `$classes` in `__autoload()` is probably being passed as `\phpexercises\entregableT3\User` which does not match your file path on disk. You may need to extract just the `User` from that string inside the `__autoload()` – Michael Berkowski Dec 28 '16 at 12:36
  • Heads up. I would not rely on `__autoload` for new code because basically [it's crap and might get deprecated soon](https://wiki.php.net/rfc/deprecations_php_7_2). Use the spl autoloader instead. – PeeHaa Dec 28 '16 at 12:37
  • I'm not using the use command. Just I added "use" after the error. Yes, all classes are on the same namespace. Just tried and echo $classname is not shown... It is not going through the _autoload function.. what i'm missing? – Albeis Dec 28 '16 at 12:38
  • `__autoload()` takes a qualified class name. In your example the `__autoload()` function takes a string `/phpexercises\entregableT3/User` and obviously can not find file with that name. – Timurib Dec 28 '16 at 13:03
  • If I comment the namespace, my echo $classname inside __autoload is "User" and then get the error. With namespace, echo statement shows anything and then the error. – Albeis Dec 28 '16 at 13:09

0 Answers0