Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dleroari's avatar

How does it work when creating new instance in php artisan tinker

General question: I am aware of that if I write php artisan tinker, and then type $faker = Faker\Factory::create(); I can access the methods in that class. However, how does tinker know exectly which class to find just by typing Faker\Factory. Do namespace have something to do with this, how is the class found? In other words, how does tinker find these classes?

0 likes
4 replies
Cronix's avatar

Yes, it uses the composer autoloader like regular laravel

Note though if you change laravel code, you have to restart tinker so it knows.

1 like
dleroari's avatar

Thanks @Cronix, but I still don't understand the connection. Is Tinker a part of composer or a feature that Laravel provides.

Cronix's avatar

Laravel uses it, but it's just a 3rd party package that anyone can use in a project, or standalone: https://github.com/bobthecow/psysh

http://psysh.org/

It's just an interactive shell for php, kind of like the commandline, but a lot easier to use than using php on the cli.

In laravel, it's aware of composer and uses the autoloader that composer provides, so it can use the namespaces and know where to find classes, etc.

Cronix's avatar
Cronix
Best Answer
Level 67

It basically loads laravel into memory, and can use whatever is available to laravel. That's why if you make code changes, you need to restart tinker so it can pick up on the changes. It only knows what's available at the time php artisan tinker is called and it launches.

Please or to participate in this conversation.