1

I've created a custom php helper class for use in my Laravel 4 project.

The SASHelper.php file is in app/libraries/elf/sas and after adding libraries into the composer.json file I've done a composer dumpautoload.

autload_classmap (last line only):

'elf\\sas\\SASHelper' => $baseDir . '/app/libraries/elf/sas/SASHelper.php',

Helper class (simplified for brevity):

<?php

namespace elf\sas;

class SASHelper
{
static public function SupportIntToText($status)
{
        return 'Supported';
}

static public function LicTypeToText($lic)
{
        return '32-Bit';
}
}

When I attempt to call either static method from my controller:

$statustext = SASHelper::SupportIntToText($client->SupportStatus);

At this point Fiddler is reporting:
"Class SASHelper not found."

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
SteB
  • 1,999
  • 4
  • 32
  • 57
  • did you try global.php? it doesn't need composer dump-autoload, and loads the class easily – Trying Tobemyself Jul 31 '13 at 13:53
  • I've just tried it: (app_path() .'/libraries' and app_path() .'/libraries/elf/sas') to ClassLoader::addDirectories but it didn't make any difference. – SteB Jul 31 '13 at 13:58
  • try commenting the namespace line – Trying Tobemyself Jul 31 '13 at 13:59
  • 1
    @TryingTobemyselfRahul - That worked (added /libraries/elf/sas path)! If you add this is an answer I'll accept, just curious, any idea why it worked (or why the other one didn't)? – SteB Jul 31 '13 at 14:07
  • 1
    best guess is when you use namespace its only available to that namespace, so when directly using class name its throwing error – Trying Tobemyself Jul 31 '13 at 14:09

3 Answers3

4

If you want to use namespaces, don't forget to composer dump-autoload!

In your case:

1 - your-folder\your-file.php

<?php
namespace elf\sas;

class SASHelper{

static public function SupportIntToText($status){
        return 'Supported';
}

static public function LicTypeToText($lic){
        return '32-Bit';
}
}

2 - config\app.php

Add to your aliases

'SASHelper' => 'elf\sas\SASHelper'

3 - run composer dump-autoload

clod986
  • 2,527
  • 6
  • 28
  • 52
1

did you try global.php? it doesn't need composer dump-autoload, and loads the class easily – Trying Tobemyself Rahul

I've just tried it: (app_path() .'/libraries' and app_path() .'/libraries/elf/sas') to ClassLoader::addDirectories but it didn't make any difference. – SteB

try commenting the namespace line – Trying Tobemyself Rahul

@TryingTobemyselfRahul - That worked (added /libraries/elf/sas path)! If you add this is an answer I'll accept, just curious, any idea why it worked (or why the other one didn't)? – SteB

best guess is when you use namespace its only available to that namespace, so when directly using class name its throwing error.

so either don't use namespace or add the namespace before the class name, in your example $statustext = elf\sas\SASHelper::SupportIntToText($client->SupportStatus); this should work

Trying Tobemyself
  • 3,668
  • 3
  • 28
  • 43
1

Said you have created a few classes under folder app/Http/xxx/yyy, and would like to autoload them.

  1. add below to composer.json:

    "autoload": {    
     "classmap": [    
          "database",  
          "app/Http/xxx/yyy"
      ],
    

    ... },

  2. run composer dump-autoload

  3. check the file exists in vendor/composer/autoload_classmap.php
ken
  • 13,869
  • 6
  • 42
  • 36