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;
}
5
  • 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 Commented Sep 20, 2011 at 8:47
  • This is really weird, it should work fine. What php version do you have? Have you checked your webserver with /app/check.php ? Commented Sep 20, 2011 at 8:54
  • Yep, it passes that with no issues Commented Sep 20, 2011 at 9:09
  • I had to manually create the "Entity" folder, do I need to do anything to make Symfony 'see' it? Commented Sep 20, 2011 at 9: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. Commented Sep 20, 2011 at 9:42

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Absolute facepalm, the perils of blindly following a tutorial.
It's an easy mistake to make with the Symfony tutorials. Personally, I'd say removing the <?php lines from the example files in the documentation is taking "don't repeat yourself" a bit too far. Incidentally, did you see the contents of the Product.php file being output before the error message? If that happens (which I think it might) that's a good clue that you've missed off the <?php.
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
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 <?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."
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.
|
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 !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.