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

dan3460's avatar

Adding a Custom Class

I'm creating a class that will handle the interaction with another site via API calls. I'm using Guzzle to interact with the other site. I look around and could not find much information about this. One of the articles that i found talks about adding the class to composer.json, do i need to do that. Is there something that i need to pay attention so it will work?

0 likes
5 replies
rodrigo.pedra's avatar
Level 56

You can create any custom class inside your ./app directory without changing anything in your composer.json file, as long the namespace is correct.

For example, let's say you want to name this class MyApiClient, you would create a php file with the same name in the ./app directory, so the file would be ./app/MyApiClient.php

inside this file you should not forget to add the namespace:

<?php

namespace App; // <- important

class MyApiClient
{
  // your code
}

When using it, for example in a controller, you just have to import it using the namespace.

<?php

namespace App\Http\Controllers;

use App\MyApiClient; // import using namespace

class TestController extends Controller
{
    public function index(MyApiClient $client)
    {
        // do something
    }
}

Composer will autoload the file correctly without needing to change anything in the composer.json file.

There are two old, but good and free, videos about composer autoloading in Laracasts:

https://laracasts.com/series/php-for-beginners/episodes/21 https://laracasts.com/lessons/psr-4-autoloading

Also, if you are a subscriber, there is a video published this week, where they build a Guzzle-based client to consume an external API:

https://laracasts.com/series/guest-spotlight/episodes/7

5 likes
dan3460's avatar

Thanks Rodrigo. I was confused why the article would say that you needed to add the class to the composer.jason. I have one question, i probably will be creating a couple more classes so i have a directory under App called classes, i'm assuming that this will be correct:

<?php
namespace App\Classes;
class my class {
...
}

Then used as:

<?php
namespace App\Http\Controllers:
use \App\Classes\MyClass;

public function theFunction() {
$myClass = new MyClass;
}
rodrigo.pedra's avatar

Yes, as long they are placed into './app/Classes/ directory, composer autoloading will find them.

One note, I believe it was a typo, the class name should match the file name and cannot contain spaces:

<?php
namespace App\Classes;
class MyClass {
// ...
}

Also, it is a convention to name classes with a capital first letter and PascalCase. It is not needed, but it is a common practice in recent PHP libraries. Just make sure your filename matches the class name and composer autoloading will find it.

For example:

If file is named: ./app/Classes/MyClass.php, the PHP class should be named:

<?php
namespace App\Classes;
class MyClass {
//...
}

If file is named: ./app/Classes/myClass.php, the PHP class should be named:

<?php
namespace App\Classes;
class myClass { // note the lower case initial
//...
}

If file is named: ./app/Classes/my_class.php, the PHP class should be named:

<?php
namespace App\Classes;
class my_class {
//...
}

And imported as such:

// in another file
use App/Classes/MyClass; // first example
use App/Classes/myClass; // second example
use App/Classes/my_class; // third example

// ...

$a = new MyClass(); // first example
$b = new myClass(); // second example
$c = new my_class(); // third example

I would stick to using PascalCase (1st example), as it is widely used nowadays and is recommended in the psr-2 standard (superseeded by the psr-12 standard).

One doubt that might arise is why the App namespacer is written with a capital A if its directory is written with a lowercase a (./app)?

Well, namespace roots are mapped to directories in the composer.json file, so if you look in your composer.json you'll see this section:

    "autoload": {
        "psr-4": {
            "App\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },

which maps the App namespace to the ./app directory.

This was just a trivia, you don't need to change your composer.json for adding custom classes inside the ./app directory.

3 likes
dan3460's avatar

wow, thanks for the tips. It was a typo. I have been using PascalCase for a while, i also think it looks better.

1 like

Please or to participate in this conversation.