Welcome to Admin Junkies, Guest — join our community!

Register or log in to explore all our content and services for free on Admin Junkies.

PHP Beginner/Intermediate - Pointer of the day.

Tyrsson

Retired Staff
Retired Staff
Joined
Apr 29, 2023
Messages
402
Website
github.com
Credits
1,098
Always use a FQCN as an identifier.

What do I mean by this? Well, here is few examples.

All too often we see things like this:

PHP:
$instance = $container->get('Some\NameSpace\DependencyClass');

Do NOT do this.Why? If for no other reason it tells static analysis and your IDE zero. 'Some\NameSpace\DependencyClass' by itself is just a string. its a code smell. Bad practice. Blah blah. You get the same results with A LOT more benefits with the following:
PHP:
<?php
// sudo code
declare(strict_types=1);

use Psr\Container\ContainerInterface;
use Some\NameSpace\DependencyClass;
use Some\NameSpace\ServiceClass;

/**
 * Usually in a factory
 * @param ContainerInterface $container
 * @return ServiceClass
 */

public function __invoke(ContainerInterface $container): ServiceClass
{
    if(! $container->has(DependencyClass::class)) {
        // throw exception
    }
    /** @var DependencyClass */
    $dep = $container->get(DependencyClass::class);
    return new ServiceClass($dep);
}

PHP:
<?php

declare(strict_types=1);

use Some\Target\ClassName;

     /** @var array<class-string, array> */
    $config = [
        ClassName::class => [
            'some_config_key' => 'some_config_value',
        ],
    ];

Take away:
Write code that it strongly typed, easy to read, and maintainable because it is testable from the start and you will have fewer issues throughout your development cycles.
 
Last edited:
This also means that PhpStorm will be able to update the code accordingly if you choose to tell it to rename or move the class.
 
Pointer of the day number 2

Put in the effort to get a debugger working in your local environment. It will probably teach you more about coding, if you learn how to use it effectively, than any other single resource. I might write a post about setting up Xdebug on the popular local environments... It's a thought.
 
Pointer of the day number 3

When developing with php. I suggest that you make sure to include a favicon in the head section and make sure that its present and accessible. This had me stumped for about 10 minutes earlier tonight as a matter of fact because my breakpoints in xdebug were being hit twice for $app->run(); Needless to say that should only happen ONCE. It was due to the favicon not being linked, so the browser, for whatever reason was making another request to the page. Happened in both FF dev edition and Chrome.
 

Log in or register to unlock full forum benefits!

Log in or register to unlock full forum benefits!

Register

Register on Admin Junkies completely free.

Register now
Log in

If you have an account, please log in

Log in
Activity
So far there's no one here

Users who are viewing this thread

Would You Rather #9

  • Start a forum in a popular but highly competitive niche

    Votes: 5 21.7%
  • Initiate a forum within a limited-known niche with zero competition

    Votes: 18 78.3%
Win this space by entering the Website of The Month Contest

Theme editor

Theme customizations

Graphic Backgrounds

Granite Backgrounds