Newbie Question about Namespace "Use" Statements

Silverstripe Version: 4 or 5

Im just embarking on learning V4 and V5 having retired from full time work at the end of life of V3 hence much as changed and the whole structure is without doubt far more complex now. However its also much faster. I have copies of both V4 and V5 working and have ported a V2 site to them as an experiment just by overloading both new versions on an existing database, I was not expecting that to work out of the box, but it did.

My real problem, comes with the changed to namespaced php which we did not have to bother with before. So in this example which is from silverstripe-lessons-v4/Lesson-21-end/app/src/PropertySearchPageController.php

How on earth do you predetermine what you need to “use” which in the above example is:

use PageController;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FormAction;
use SilverStripe\ORM\ArrayLib;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\ORM\PaginatedList;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;
use SilverStripe\Control\HTTP;

Stupid newbie question I know, but it seems you have to know the structure inside out to guess these or is there a cheat sheet of some sort?

In the top of the API documentation there is a class field that always specifies the namespace.

1 Like

This is where a good code editor is pretty much essential for any modern PHP application. I’m a PHPStorm user, but you also have the choice of things like VSCode, Netbeans, etc.

Most of the editors will automatically import / add the class names in the use statements as you use them in the code, so you don’t need to worry about writing any of that manually.

It is possible to derive them by working your way through the vendor directory, but life is too short for that kind of thing!

1 Like

Thanks for that great heads up!! Im almost 70 so I need a kickstart!!