Passing a class as function parameter

Passing a class as function parameter

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Passing a Class as Function Parameter in PHP

In Laravel, you often encounter scenarios where a utility function needs to interact with multiple Eloquent models. Consider a helper method designed to generate unique slugs. It might need to query the products table for app slugs and the apk table for application package slugs. The natural instinct when refactoring this logic is to pass the model class directly into the function signature, as shown in your example:

public function uniqueSlug($str, Apk) {
    $slug = Str::slug($str);

    // Querying the passed class dynamically
    $count = Apk::whereRaw("slug RLIKE '^{$slug}(-[0-9]+)?$'")->count();

    return $count ? "{$slug}-{$count}" : $slug;
}

However, this approach relies on PHP's dynamic variable resolution. When you pass Apk (a class name or the model instance) as a parameter, the function receives it as an argument. If you intend to access static methods like Apk::whereRaw(), the syntax inside the function body must reflect exactly how that class is resolved at runtime.

If $App is passed as a string containing the fully qualified class name (e.g., 'App\Models\Apk'), you can use the backtick operator or eval() to execute code against that class, though using dynamic evaluation in Laravel is generally discouraged due to security risks and static analysis limitations. A more robust pattern involves passing the model instance itself:

public function uniqueSlug($str, $model) {
    $slug = Str::slug($str);

    // Using the passed object directly
    $count = $model::whereRaw("slug RLIKE '^{$slug}(-[0-9]+)?$'")->count();

    return $count ? "{$slug}-{$count}" : $slug;
}

This pattern is technically valid in PHP. You can pass a model instance (or the class name if it resolves correctly in scope) and invoke static methods on it within the function. However, there are significant trade-offs to consider regarding maintainability and type safety.

Type Safety and Static Analysis

Modern Laravel development relies heavily on static typing and tools like PHPStan or Rector to catch bugs early. When you pass a class name as a string (e.g., 'Apk') into a function, the type checker cannot determine what that class represents without complex reflection logic. This breaks the flow of information that helps developers understand how data moves through your application architecture.

Instead of passing raw class names or relying on dynamic resolution, it is often cleaner to accept an instance of the model or utilize dependency injection to resolve the correct repository or query builder within the helper itself. If you must keep the function generic across multiple models, consider using a trait or interface that defines the scope logic, rather than trying to pass the class dynamically.

Security Considerations

Your example uses whereRaw with string interpolation inside the RLIKE pattern:

$slug = Str::slug($str);
$count = Apk::whereRaw("slug RLIKE '^{$slug}(-[0-9]+)?$'")...

While this is safe if $str is strictly validated, passing a dynamic class name into a function that executes raw SQL constructs adds complexity. If you attempt to resolve the class name dynamically using eval() or similar techniques to avoid passing instances, you open the door to serious security vulnerabilities. Always prefer explicit type hints over dynamic resolution where possible.

Practical Implementation

If you find yourself needing this flexibility often, consider creating a dedicated service class that accepts the model instance and handles the unique slug logic:

// SlugHelper.php
public function generateUniqueSlug(string $str, ModelInterface $model): string
{
    $slug = Str::slug($str);
    
    // Query the specific table associated with the passed model instance
    $count = $model::whereRaw("slug RLIKE '^{$slug}(-[0-9]+)?$'")->count();

    return $count ? "{$slug}-{$count}" : $slug;
}

By passing $model as an instance rather than a class name, you maintain type safety, allow for better IDE autocomplete within the function body, and avoid the pitfalls of dynamic code execution. This approach aligns with Laravel's conventions while keeping your codebase secure and readable.

Our network