0

There are questions on here and around Google asking about the same thing, but being a noob, I'm still not getting this. I'm using Laravel 4.

Trying to have a file for random classes. Doesn't load.

The class is in:

app/classes/Helpers.php

Helpers.php:

class Helpers {

    public function randomLowerCase($amount)
    {
        strtolower($str_random($amount))
    }

};

I've placed my classes in composer.json.

"classmap": [
    "app/commands",
    "app/controllers",
    "app/models",
    "app/classes",
    "app/database/migrations",
    "app/database/seeds",
    "app/tests/TestCase.php"
]

autoload_classmap.php:

'Helpers' => $baseDir . '/app/classes/Helpers.php',

And also ran

composer dump-autoload

I'm running the function in a UserController.php file in controllers, but I keep getting Call to undefined function randomLowerCase()

2
  • Are you running (instance of Helpers)->randomLowerCase()? It sounds like you're running randomLowerCase() without the class... Commented Nov 19, 2013 at 3:46
  • Do you call like this. $helpers = new Helpers;$helpers->randomLowerCase(); Commented Nov 19, 2013 at 4:50

1 Answer 1

1

The problem is that you're not instantiating an instance of the Helpers class before you call one of its methods. You'll want to do one of the following:

First, keeping your class as it is, you could create an instance in the controller and call your method on it:

// Controller
$helpers = new Helpers;
$helpers->randomLowerCase($str);

Or, you could make the method static and call it as a static method:

// Helpers.php
class Helpers
{
    public static function randomLowerCase($amount)
    {
        strtolower($str_random($amount))
    }

};

// Controller
Helpers::randomLowerCase($str);

The error you're getting is because you're running the randomLowercase method as if it were just a function; methods are functions attached to a class/object.

Sign up to request clarification or add additional context in comments.

2 Comments

Tried both solutions, but the error is the same :( Am I loading it right with composer/autoload?
I'm a derp. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.