Undefined class if class is in different namespace

php namespace class not found
phpstorm import class shortcut
php autoload namespace
php use
php class not found
php global namespace
spl_autoload_register
php namespace best practices

I have a yii2 project set up on phpstorm 8.0.3. My namespace structure is as follows:

backend\
  controllers
  models
  ...
frontend\
  controllers
  models
  ..
common\
  controllers
  models

I used composer on the project and have various dependancies installed in the vendor folder.

Whenever I try to use a class that is not in the same base namespace (frontend, backend or common), I get an undefined class error. The odd thing is the namespace of the class is in autocomplete:

Another oddity is that the undefined class error disappears for some classes if the file with the defined class is opened.

It looks like the PHPStorm cache was somehow corrupted. I ended up invalidating the cache by going to File -> Invalidate Caches/Restart. This deleted my history but fixed the undefined class issue.

Undefined class if class is in different namespace, The odd thing is the namespace of the class is in autocomplete: Another oddity is that the undefined class error disappears for some classes if the file with the  1. You create your own source control class which inherit from %Studio.SourceControlBase 2. you put your code in that class. For example you may use OnAfterSave method to run on all namespaces that your class need to be saved, loop on this list (except the current one of course) and save + compile your class (programmatically) in each namespace.

You must declare in use section nedded class or namespace. For example, if you need to use BaseController from custom\controllers namespace:

<?php
namespace frontend\components;

// Add this line
use common\controllers\BaseController;

class Controller extends BaseController {
}

FAQ: things you need to know about namespaces - Manual, If I don't use namespaces, should I care about any of this? Undefined Constants referenced using any backslash die with fatal error · Cannot override special constants another way to use a class from the current namespace as a type hint identifier "class" is undefined -- compiling C++ as C? The compiler is spitting out a long list of errors, all of which would occur if a C++ file were compiled as if it were C. Files causing errors are all of my cpp header files, all the mbed header files.

I think you have some problems with composer autoload. Try :

composer update

Autoloading Classes - Manual, Many developers writing object-oriented applications create one PHP source file per class If the class name is used e.g. in call_user_func() then it can contain some The following function may be useful if you want to simulate namespaces and You can enable this behaviour for undefined classes while unserializing  Fixes google#4883 Fixes google#3927 Individual commits: * remove "static" from soon-to-be method functions * move almost all functions into class as methods * set current namespace and emit package names if needed * track imported namespaces * parent namespaces work * docker test for go ^1.11 * update base image name for go docker test * remove

This was happening to me because of my PHP language settings. I found this out by taking out the namespace in one of my classes. The error changed to "Scalar types are only available in PHP 7".

Go to File -> Settings -> Language & Frameworks -> PHP. Change the PHP language level to language version 7 or greater.

Edit: After doing that, Insert -> Getters and Setters was intermittently generating functions and comments as mixed instead of string. I don't know why this happened but I recreated all my fields manually and that fixed the issue.

Auto import - Help, When you reference a PHP class that is defined outside the current file, Automatically complete the short class name and import the namespace the class is defined and any other symbols that can be exported, as well as for XML namespaces. not highlighted by the Undefined class or Undefined method inspections),  The prefix is a string used to reference the namespace within an XML object. If the prefix is undefined, when the XML is converted to a string, a prefix is automatically generated. Namespace to differentiate methods Namespaces can differentiate methods with the same name to perform different tasks.

Namespaces, Specify a single class per use statement. Do not specify multiple classes in a single use statement. If there are multiple use declarations in a file,  @fortyff said in Undefined referance to class::function: There are the same errors occured in the "esimplelabel.cpp" file, but I didn't write down here, since it'll consume too much place. Make sure you have all virtual functions that are declared defined.

Namespaces, Multiple namespace blocks with the same name are allowed. 4) Namespace names (along with class names) can appear on the left hand side as if declared in the same class scope, block scope, or namespace as where this If an entity is declared, but not defined in some inner namespace, and then  Now among them i need to call a class which is under a namespace in visual studio 2008. Please don't ask me to add the dependencies to the main project so that i can access the namespace and all the classes in that just like local namespace which i can't do due to some restrictions.

Ambient Namespaces, This post outlines the various ways to organize your code using namespaces in we want the interfaces and classes here to be visible outside the namespace, we the names of the types when used outside the namespace, e.g. Validation. QName represents a qualified name as defined in the XML specifications: XML Schema Part2: Datatypes specification, Namespaces in XML, Namespaces in XML Errata. The value of a QName contains a Namespace URI, local part and prefix. The prefix is included in QName to retain lexical information when present in an XML input source.