This is part of the series tinker like a boss (in psysh)
Aliases
Class aliasing is very simple concept, that allows you to call a class by its alas, rather than original name. It’s not very common to use aliases when writing code , for your IDE gets you covered.
If you’re working with Laravel, it’s worth mentioning that all the facades are also aliases, that’s why you have things like request, validator etc available in global namespace. this is a convention introduced by Taylor to make things a bit more expressive,but still something that your IDE does for you.
As we know psysh offers TAB autocompletion, but still typing full namespaced class names gets tedious and unfortunately it doesn’t work will every class in your app. That’s wher custom aliases come in handy, because in REPL you type everything by yourself.
Using already mentioned .tinker
include file, you can easily alias all the classes you need:
1 2 3 4 5 6 7 8 9 |
<?php // register single, custom alias _Tinker::alias('A\Long\Namespace\Calculator', 'Calc'); // register classnames as aliases from a path _Tinker::register(app_path('Models')); |
Of course we can do that at runtime as well:
1 2 3 4 5 6 |
➜ ldk git:(master) ✗ php artisan tinker Psy Shell v0.7.2 (PHP 7.0.10-1+deb.sury.org~trusty+1 — cli) by Justin Hileman >>> _Tinker::register(app_path('Models')) |
If you didn’t check it yet, here is the psysh config file that I prepared for you.
Enjoy and share your thoughts in the comments below or on twitter @SOFTonSOFA
Now is the time to go to your console, type $ php artisan tinker
and follow along
Laravel – tinker like a boss with PsySH from Jarek on Vimeo.