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

Digisol's avatar

Helper not recognizing Classes

I am attempting to write a Helper function to create graphs. I have included my app/helper.php file in the composer autoload, however when I attempt to run the following code, the classes aren't found.

<?php 

use App\Amenadiel\JpGraph\Graph;
use App\Amenadiel\JpGraph\Plot;

 function create_pie_graph(array $data, array $dimensions, string $title, string $file_name){
    $colours = ['#8D201C', '#EE8310', '#2A7DA6', '#5A306B', '#285228'];
    $graph = new Graph/PieGraph($dimensions[0],$dimensions[1]);
    $graph->title->Set($title);
    $graph->SetBox(true);

    $pie  = new Plot\PiePlot($data);
    $pie->ShowBorder();
    $pie->SetColor('black');
    $pie->SetSliceColors($colours);
    $graph->Add($pie);

    if(file_exists(storage_path('app/public/charts/'.$file_name))){
        unlink(storage_path('app/public/charts/'.$file_name));
      }
      $graph->Stroke(storage_path('app/public/charts/'.$file_name));
}
0 likes
8 replies
Digisol's avatar

@vincent15000 I've changed the helper to a class GraphCreation.php

"autoload": {

        "psr-4": {
            "App\": "app/",
            "Database\Factories\": "database/factories/",
            "Database\Seeders\": "database/seeders/"
        },
        "files": [
            "app/Helpers/GraphCreation.php"
        ]
    
    }

The new code for the class follows

<?php 

namespace App\Helpers;
use App\Amenadiel\JpGraph\Graph;
use App\Amenadiel\JpGraph\Plot;

class GraphCreation{

  public static function create_pie_graph(array $data, array $dimensions, string $title, string $file_name){
    $colours = ['#8D201C', '#EE8310', '#2A7DA6', '#5A306B', '#285228'];
    $graph = new Graph/PieGraph($dimensions[0],$dimensions[1]);
    $graph->title->Set($title);
    $graph->SetBox(true);

    $pie   = new Plot\PiePlot($data);
    $pie->ShowBorder();
    $pie->SetColor('black');
    $pie->SetSliceColors($colours);
    $graph->Add($pie);

    if(file_exists(storage_path('app/public/charts/'.$file_name))){
        unlink(storage_path('app/public/charts/'.$file_name));
      }
      $graph->Stroke(storage_path('app/public/charts/'.$file_name));
  }
}
Sinnbeck's avatar

@Digisol that's a class, so no need to load it. It follows composer autoload already. Just make sure the folder is the same as the namespace

How do you use it?

jaseofspades88's avatar

I would advise you create its own class. If you're going to throw pie chart creation into the helper file, where will it stop?

jlrdw's avatar

@Digisol just verify all your "use" statements are correct and dumpautoload.

Digisol's avatar

@jlrdw I've attempted that, I reference the problem classes:

use Amenadiel\JpGraph\Graph;
use Amenadiel\JpGraph\Plot;

In my controller using this code, and they work fine from within the controller. I've never had this problem before, whenever I have referenced classes from composer in a helper before everything worked fine. I'm completely at a loss for what's wrong.

jlrdw's avatar

@Digisol put Amenadiel\JpGraph\Graph in correct folder, Helpers, and namespace it correctly.

use App\Helpers\Amenadiel\JpGraph\Graph;

namespace is:

App\Helpers\Amenadiel\JpGraph

Please or to participate in this conversation.