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

Rediska's avatar

How can I include a php file in a function?

I'll try to explain my need more simply) I have several parsers, each of which has its own file with the necessary functions. When the function starts, I need to load this file with functions for it to work. How can I do this?

Let's assume a file with functions like this:

<?php

function getCategory ($catId) {
    if ($catId == '123') {
        return 'phones';
    } else {
        return 'laptops';
    }
}

The method in the controller looks like this:

class ParserController extends Controller
{
    public function getData($id)
    {
        $model = Parser::find($id);
        include(Storage::disk('local')->get('parsers/functions/' . $model->id . '-' . $model->title . '.php'));
        dd(getCategory('123'));
    }
}

I'm getting this error. It seems to be reading the file, but I can’t use the functions from it. ErrorException include(&lt;?php function getCategory ($catId) { if ($catId == '123') { return 'phones'; } else { return 'laptops'; } } ): failed to open stream: No error What am I doing wrong?

0 likes
8 replies
jlrdw's avatar

With the use statement.

If static methods that's all you need. If instance methods you need a new instance of the class injected.

If straight php, lookup the require and include statements.

Take the php for beginners free course from here.

Snapey's avatar

This is a pretty stinky code smell, but,

include(Storage::disk('local')->path('parsers/functions/' . $model->id . '-' . $model->title . '.php'));

You should test if the function exists before calling the function and catch errors

But really? Include files? I am sure there are MULTIPLE better ways to do this

Rediska's avatar

@Snapey I smell this smell too)) But I can’t choose a more suitable option. And I don’t understand why it doesn’t work with include

martinbean's avatar

@rediska Why not just use classes and methods properly?

Encapsulate your logic in a proper class, instead of trying to horribly include random functions from random files.

Rediska's avatar

@martinbean I have a shared php file. I create a new class and use it as expected.

<?php

namespace App\Modules;

use Illuminate\Support\Arr;

class Functions
{
static function getSale($oldPrice, $price)
    {
        ...
    }
}

And I address him this way:

Functions::getSale(100,50);

It works. But now I have a different task. I can't use use App\Modules\Functions; in the controller or model. I only need to load one file, the name of which I define in the function itself.

krisi_gjika's avatar

@rediska maybe something like this would make more sense? First create your parser functions as classes, than add a new column on your parsers table with the $parser::class namespace for each parser.

class Parser extends Model
{
	public function parse($input)
	{
		$parser = app($this->handler); // $parser->handler = App\Modules\Parsers\CategoryParser;
		return $parser->parse($input); 
	}
}
```	

Please or to participate in this conversation.