6

I've got Symfony 2 successfully installed and set up and have been following the documentation through.

I'm currently up to http://symfony.com/doc/2.0/book/doctrine.html

Everything is fine until I get to this line:

php app/console doctrine:generate:entities Acme/StoreBundle/Entity/Product

at which point I get the following error:

[RuntimeException]

The autoloader expected class "Acme\StoreBundle\Entity\Product" to be defined
in file "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\Symf
ony\app/../src\Acme\StoreBundle\Entity\Product.php". The file was found but the
class was not in it, the class name or namespace probably has a typo.

This has happened to me on both Linux and Windows machines.

The contents of Product.php is as per the tutorial:

// src/Acme/StoreBundle/Entity/Product.php
namespace Acme\StoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="product")
 */
class Product
{
     /**
      * @ORM\Id
      * @ORM\Column(type="integer")
      * @ORM\GeneratedValue(strategy="AUTO")
      */
     protected $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $name;

    /**
     * @ORM\Column(type="decimal", scale=2)
     */
    protected $price;

    /**
     * @ORM\Column(type="text")
     */
    protected $description;
}
j0k
  • 22,600
  • 28
  • 79
  • 90
Thom Shutt
  • 449
  • 5
  • 17
  • can you show what's inside C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\Symf ony\app/../src\Acme\StoreBundle\Entity\Product.php – Inoryy Sep 20 '11 at 08:47
  • This is really weird, it should work fine. What php version do you have? Have you checked your webserver with /app/check.php ? – Inoryy Sep 20 '11 at 08:54
  • Yep, it passes that with no issues – Thom Shutt Sep 20 '11 at 09:09
  • I had to manually create the "Entity" folder, do I need to do anything to make Symfony 'see' it? – Thom Shutt Sep 20 '11 at 09:28
  • @Thom You shouldn't; the error message suggests that the file is being found successfully ("The file was found..."), so it looks to me like Symfony can see it just fine. – Matt Gibson Sep 20 '11 at 09:42

2 Answers2

25

That message comes from DebugUniversalClassLoader.php:

public function loadClass($class)
{
    if ($file = $this->findFile($class)) {
        require $file;

        if (!class_exists($class, false) && !interface_exists($class, false)) {
            throw new \RuntimeException(sprintf('The autoloader expected class "%s" to be defined in file "%s". The file was found but the class was not in it, the class name or namespace probably has a typo.', $class, $file));
        }
    }
}

So, the file must be there, and readable, otherwise the findFile and the require wouldn't have worked. All this check is doing is require()ing the file and then using the standard PHP class_exists() to see if the class is now there.

The only thing I can think of that could cause this message given those file contents: you've missed off the <?php at the start of the file. Now, I know this is going out on a bit of a limb, but I honestly can't think of anything else that wouldn't cause some other kind of error.

Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
  • 5
    Absolute facepalm, the perils of blindly following a tutorial. – Thom Shutt Sep 20 '11 at 09:57
  • It's an easy mistake to make with the Symfony tutorials. Personally, I'd say removing the ` – Matt Gibson Sep 20 '11 at 10:00
  • 1
    Indeed, I think I'll put the recommendation to them. I did see the contents being printed and won't be forgetting what that means any time soon :) Thanks for your help – Thom Shutt Sep 20 '11 at 10:07
  • Yup, that was it! "The only thing I can think of that could cause this message given those file contents: you've missed off the – Samurai Ken May 24 '12 at 09:30
  • ugg.. me too. The "autoloader expected class .. to be defined in file" and "probably has a typo" were red herrings. Easy to forget after being away from php for a while. – toddkaufmann Feb 23 '13 at 14:54
  • Had the same problem.. I used the short open tag and my OSX `/usr/bin/php` interpreter didn't recognize it while MAMP didn't had any problems.. – Micronax Feb 06 '14 at 22:40
  • @MxAgent Ah, yeah, there will be two different php.ini files for MAMP and for the standard OS X PHP, so it's possible to have short tags turned on in one but not the other. (Common practice with Symfony seems to be to have short tags turned off; it's also generally recommended if you're going to be distributing your PHP for other people, as you can't guarantee another server has them turned on. I'd recommend having them turned off, personally, but it's [mostly a matter of religion](http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use)) – Matt Gibson Feb 07 '14 at 07:57
1

I had the same error "The file was found but the class was not in it, the class name or namespace probably has a typo.". It's only because

<?php

is missing at the beginning of the file !! Argh, copy-paste from a tutorial... Bye !

vinsse2001
  • 259
  • 3
  • 3