use not working in require classes - php

I created class named Task in the App\Entity namespace and my problem is in my another class I want to use it but it doesn't detect my class and give me error:
Attempted to load class "Task" from namespace "App\Entity".
Did you forget a "use" statement for another namespace?
this my Task.php:
namespace App\Entity;
class Task
{
protected $task;
public function getTask()
{
return $this->task;
}
public function setTask($task)
{
$this->task = $task;
}
}
This is my DefaultController that uses the Task class:
use App\Entity\Task;
class DefaultController
{
}

Depending upon your autoloader settings, if you are using composer, you can have it such that autoloading only happens from the pre-built class map - called 'authoritative classmap' (https://getcomposer.org/doc/articles/autoloader-optimization.md).
So, when you add a class to your source, it isn't recognised by the pre-built autoloader. Try running composer dump to let the autoloader load via standard PSR-0/PSR-4 rules.
If you are using the autoloader on PSR-0/PSR-4 mode, then maybe you've not put the file in the right directory.

Related

Dynamically Initiate a class in another's constructor in Laravel (or php)

To make it simple i have two classes:
class AddressController extends ApiController
{
private AddressRepository $addressRepository;
public function __construct(AddressRepository $addressRepository)
{
$this->addressRepository = $addressRepository;
}
//........
class CountyController extends ApiController
{
private CountyRepository $countyRepository;
public function __construct(CountyRepository $countyRepository)
{
$this->countyRepository = $countyRepository;
}
//........
As you can see I'm extending the ApiController class and use dependency injection for both (county/address) repository.
My question is how to refactor it in a away everytime i extend from the ApiController, it create the repository property with the proper namespace.
Hi I suggest to do some tweaks on your Laravel Stub, which can fulfill your requirement.
On publishing Laravel Controller from command, you can customize your namespace and the stuffs you require in controller.
You can publish your current stub file using artisan command
php artisan stub:publish
for more https://laravel-news.com/customizing-stubs-in-laravel

Laravel / Lumen - Reflection Exception Class does not exist

I am trying to inject a Manager class into toe Service Container of Lumen. My goal is to have a single instance of LogManager which is available in the whole application via app(LogManager::class).
Everytime i try to access this shortcut i get the following exeption:
[2017-03-23 16:42:51] lumen.ERROR: ReflectionException: Class LogManager does not
exist in /vendor/illuminate/container/Container.php:681
LogManager.php (i placed that class in the same location where my models are (app/LogManager.php))
<?php
namespace App;
use App\LogEntry;
class LogManager
{
...
}
AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\LogManager;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->singleton(LogManager::class, function ($app) {
return new LogManager();
});
}
}
I uncommented the line $app->register(App\Providers\AppServiceProvider::class); in bootstrap/app.php
I think that i missed something with the correct namespacing or placement of the classes espaccially LogManager. Maybe some one is willing to give me a hint?
If you need some more informations just give me a hint!
Your class and your service provider look fine. However, wherever you're calling app(LogManager::class) also needs to know the fully qualified name of the class.
Either make sure you have use App\LogManager at the top of the file, or change your call to app(\App\LogManager::class).

Using a namespaced class as a View Composer in Laravel 4?

I have a simple setup where I need to show a bit of data universally across my app, in the header of my site. To do so, I have created a ComposerServiceProvider class and a HeaderComposer class to delegate this responsibility, as seen below:
ComposerServiceProvider
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider {
public function register() {
$this->app->view->composer('templates.header', 'HeaderComposer');
}
}
HeaderComposer
class HeaderComposer {
public function compose($view) {
Event::listen('illuminate.query', function($q) {
print_r($q);
});
$view->with('nearbyMissions', array(
'past' => Mission::remember(60, 'previousMissions')->previousMissions(3)->get(),
'future' => Mission::remember(60, 'nextMissions')->nextMissions(3)->get()
));
}
}
Previously, these classes were not namespaced, but I have now decided to namespace each of them as a good practice, by prepending the files with:
namespace MyProject\Composers;
However, this has broken my application as some part of my project can no longer resolve my composer classes. None of my pages work because they all use a templated header view which uses my HeaderComposer:
Class HeaderComposer does not exist (View: H:\myproject\app\views\templates\main.blade.php)
Where am I meant to declare the use statements for my class? In my view? (Which doesn't seem right). Somewhere else?
Since you just namespaced your class, then you have to update your composer binding:
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider {
public function register() {
$this->app->view->composer('templates.header', 'MyProject\Composers\HeaderComposer');
}
}

Attempted to load class "ClassName" from namespace (...). Even though namespace is imported

I have a Symfony project to which I added some non-symfony php files containing various classes. But for some reason the classes are not loaded when loading the website, even though the IDE sees them properly.
So, I have a class that needs other classes:
namespace rootspace\FrontBundle\Controller;
use rootspace\FrontBundle\Networks\TwitterOAuth;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class TwitterController extends Controller
{
public function connectAction(){
// The TwitterOAuth instance
$connection = new TwitterOAuth('abc', '123');
}
}
And then the class which fails to load (that needs yet another file)
namespace rootspace\FrontBundle\Networks;
/* Load OAuth lib. You can find it at http://oauth.net */
//require_once('OAuth.php'); -- should this be commented out?
/**
* Twitter OAuth class
*/
class TwitterOAuth {
/* Contains the last HTTP status code returned. */
}
Lastly, the third file
namespace rootspace\FrontBundle\Networks;
use Symfony\Component\Config\Definition\Exception\Exception;
class OAuthConsumer
{
public $key;
public $secret;
}
(...)
I assume the actual filenames don't matter, right? Nor their structure? PhpStorm sees all the classes properly, I can right-click through them, but it fails when deployed.
Thanks for help
Edit - the whole error message says
Attempted to load class "TwitterOAuth" from namespace "rootspace\FrontBundle\Networks" in D:\Dropbox\project\src\rootspace\FrontBundle\Controller\TwitterController.php line 15. Do you need to "use" it from another namespace?
This is because Symfony's autoloader follows PSR standards (PSR-0, PSR-4) which says that fully qualified (with namespace) class name translates to file location and name. So in fact file names does matter.
So in your case rootspace\FrontBundle\Networks\TwitterOAuth class should be located in rootspace/FrontBundle/Networks directory in file called TwitterOAuth.php
If classes you are using does not follow PSR standards you can also register them manually in app/autoloader.php file
Check these for more info:
How can I add a namespace to Symfony 2.1?:
How to autoload class
And check this answer
I forgot to add a .php extension to my filename

PHP Namespace Class

I read up quite a bit on namespaces in PHP and I'm still confused.
I have a class in a different folder that is under the namespace Entity (Class A).
I have another class in a different folder that is under the same namespace (Class B), and extends class A.
I get an error saying class A could not be found.
My main question is - do I have to include class A when I create a new instance of class B?
This is my code:
(Class A)
namespace Entity;
//Framework/Entity/BaseModel.php
class BaseModel {
//TODO: IMPLEMENT THIS
public function GetList() {
return null;
}
}
(Class B)
namespace Entity;
//Models/Points.php
class Points extends BaseModel{
public $Id = null;
}
(Main File)
require_once(dirname(dirname(__FILE__)) . '/Models/Points.php');
$points = new Points();
Namespaces have nothing to do with actually including files, those are two completely separate mechanisms. So, yes, you will still have to require_once the file that the class is defined in before you can use it.
Having said that, especially with namespaces, autoloaders are typically used so you don't have to write a ton of require code. If you organise your class files in folders exactly as their namespaces are, it's very easy to autoload their files. See http://php.net/manual/en/language.oop5.autoload.php and https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
Parent class definition must be know on definition of child class.
Even before you create instance of child class.
How otherwise PHP would know what to put inside class you create?
require_once('BaseModel.php')
namespace Entity;
//Models/Points.php
class Points extends BaseModel{
public $Id = null;
}

Categories