0

I've got the following problem:

This is my super basic class:

class A
{
    function foo()
    {
        echo "bar";
    }
}

Now in front of the class declaration I use the following code:

$a = new A();
$a->foo();

When I open the php file in the browser, the output is "bar". Fine!

Now I want to do the same thing in another file.

directly in the first place I declare the following namespace:

namespace model\dbAction;

This is the path where my file with the class above is located.

So in another php file I do the following:

$a = new \model\dbAction\A();
$a->foo();

But I don't get any output and other code after that won't run so it looks like it breaks directly after the instancing of the class.

Any ideas why instancing the class in another file is not working?

Thanks!

Full code first php file:

<?php
namespace model\dbAction;
class A
{
    function foo()
    {
        echo "bar";
    }
}

Full code of the second file (which I call in the browser):

$a = new \model\dbAction\A();
$a->foo();
7
  • 1
    Do you have namespace A; above that class? Commented Jan 7, 2015 at 14:32
  • enable display_erros and error_level E_ALL Commented Jan 7, 2015 at 14:32
  • The namespace is model\dbActions which is the path to the file. And it is declared right in the beginning before I declare the class. When I enable errors I get the following error: Fatal error: Class 'model\dbAction\A' not found in /var/www/dgroupReporting/install/install.php on line 7 Commented Jan 7, 2015 at 14:36
  • 1
    Show all of the code. Did you include the class file in the new file? Commented Jan 7, 2015 at 14:37
  • 1
    You probably didn't included a file. Or have broken autoloader Commented Jan 7, 2015 at 14:42

1 Answer 1

1

You still need to include the file -- providing the namespace itself will not include the file for you... unless you're using an autoloader. See: How do I use PHP namespaces with autoload?

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

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.