Symfony class not found but the class is defined - php

I've created a class in symfony, the file path is
src/Project/MemberBundle/Document/EnumMemberType.php
and the file looks like this
<?php
namespace Project\MemberBundle\Document;
class EnumMemberType {
const VIP = 'xxxxx';
} // class - EnumMemberType
but when i try to get the constant in this class in another file using the code
$memberType = \Project\MemberBundle\Document\EnumMemberType::VIP
i got the error
Attempted to load class "EnumMemberType" from namespace "Project\MemberBundle\Document".
Did you forget a "use" statement for another namespace?
I really can't figure out why, the file name is the same with the class name, and it's under the right folder, and the file starts with <?php.
Can anyone see what else i'm missing? Thanks

Maybe problem with namespace. You can write in the header of another file:
use Project\MemberBundle\Document\EnumMemberType
and
$memberType = EnumMemberType::VIP;

Related

Laravel: Target class does not exist. But it does

I've searched for answers, but mostly the problem was a typo in class or a controller.
In my case everything is spelled properly. Class in app\Http\Controllers\GenerateTextController.php:
<?php
namespace App\Http\Controllers;
class generateText extends Controller
{
public function generate()
{
dd('success');
}
}
then I try to inject it into blade. home.blade.php :
#inject ('generate', 'App\Http\Controllers\GenerateTextController')
#dd($generate)
Result:
Target class [App\Http\Controllers\GenerateTextController] does not exist.
I have already composer autoloaded couple of times, artisan cache cleared, nothing helps. I can't even find a closest solution in web.
Interesting thing: When I try dd on the other class, that was, how to say, "predefined" by Laravel - it shows the class. And my second custom class can be viewed in browser with such injection.
Any help appreciated.
Your class file name is app\Http\Controllers\GenerateTextController.php but the class name is generateText, that's the problem. The class name and the file name should match. This is how, PSR-4 autoloader works.
From the Specification:
The terminating class name corresponds to a file name ending in .php.
The file name MUST match the case of the terminating class name.
So the class name should be like:
class GenerateTextController extends Controller
{
// ...
}
Read about PSR-4 autoloader to understand it.
Classes file name should be the same as the PHP class name.
class GenerateTextController extends Controller

Symfony2.7, adding 3rd party lib to project

I'm stuck, i wanted to load external library to my symfony2 project but got error stating that class was not found my app/autoloader.php:
...
$loader->add('Tinify', __DIR__.'/../vendor/tinify/tinify/lib');
...
and my file where i want to use it looks like it:
<?php
namespace XYZ\NewsBundle\Controller;
...
use Tinify;
class NewsController extends Controller{
...
public function displayAction($slug)
{
$em = $this->getDoctrine()->getManager();
$external = new \Tinify();
}
error is as follow The autoloader expected class "Tinify" to be defined in file "xyz/app/../vendor/tinify/tinify/lib\Tinify.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
but file under vendor\tinify\tinify\lib\Tinify.php
namespace Tinify;
const VERSION = "1.3.0";
class Tinify {
...
}
i checked if it really has typo but don't see one
Full qualified class name of Tinify is not Tinify but \Tinify\Tinify. Its namespace + classname.
In you NewsController class you should do:
use \Tinify\Tinify;
Also note the backslash at the beginning of the namespace.
Then in the code you should use just class name and not namespace so also change this:
$external = new \Tinify();
to this:
$external = new Tinify();
Why don't install Tinyfy throught Composer?
composer require tinify/tinify
In this way composer handles de autoload of the library, you don't need to load manually nothing, you only must to make an instance of the class and run
$tinify = new Tinify();

Converting php code to Symfony class

i am using a simple php code with Activexpert to send sms here the intersting part
<?php
if(isset($_POST["submit1"]))
{
$_objSmsProtocolGsm = new Com("ActiveXperts.SmsProtocolGsm");
...
}
?>
it working fine but when i tried to insert it into my controller in symfony
i get an error
Attempted to load class "Com" from namespace
"PFE\SiivtBundle\Controller" in C:\Program Files
(x86)\EasyPHP-DevServer-14.1VC11\data\localweb\Symfony2.5\src\PFE\SiivtBundle\Controller\SiivtController.php
line 835. Do you need to "use" it from another namespace?
while my php code is simple and doesnt include or require any other files
Error message is self-explanatory.
Com class doesn't exist in PFE\SiivtBundle\Controller namespace (which is current namespace in your controller class).
Try:
new \Com("ActiveXperts.SmsProtocolGsm");
The backslash before classname means that we're looking for a class in global namespace

PHP namespace and include files

I have the following PHP code:
<?php
namespace my_ns;
class dummy
{
function do_print(){echo "printing...";}
}
$obj_dummy=new dummy();
$obj_dummy->do_print();
?>
This works all fine,
How ever If I put the class in a separate include file (e.g. class.dummy.php), making the code on the page look like below:
<?php
namespace my_ns;
include ("class.dummy.php");
$obj_dummy=new dummy();
$obj_dummy->do_print();
?>
I get the error message:
Class 'my_ns\dummy' not found in ...
How can by default make sure that (all) include-files are automatically added to a given namespace?
A file containing a namespace must declare the namespace at the top of the file before any other code - with one exception: the declare keyword.
Source
Add this to your included file as well.
namespace my_ns;
After that, your code works just fine.
Reference

namespaces and class extends

Im trying to figure out how namespaces works in PHP, but havent really been lucky
Hope somebody could tell me what Im doing wrong here :)
code
require_once 'Vatcode.php';
$Vatcode = new \resource\Vatcode();
Vatcode.php
namespace resource;
require_once Ini::get('path/class').'/Resource.php';
class Vatcode extends Resource {
public function __construct(){
echo 'works!';
}
}
Rescource.php
namespace resource;
class Resource {
}
error
Fatal error: Class 'resource\Ini' not found in Vatcode.php
it's just a problem of namespace.
Your class Vatcode is in namesapce ressource. If, in the file of VatCode declaration you use nameofclas::... or new nameofclass() it will try to get the class in namespace ressource.
If you want to use the class Ini inside your document you have two solutions :
first give the full qualified name :
require \namespace\of\ini\Ini::get('path/class').'/Resource.php';
second using the "use" keyworld before using the get method :
use \namespace\of\ini\Ini;
require_once Ini::get('path/class').'/Resource.php';
In any case, if Ini is in "no namespace" (global namespace is the accurate word) you just has to use the solutions I gave you but only with \Ini instead of \namespace\of\ini\Ini

Categories