icc97
icc97

Reputation: 12783

PSR-1 2.3 Side Effects Rule example

Following on from this other question.

In the PSR-1 Guidelines, the section 2.3 Side Effects rule restricts using include and declaring functions in the same file, e.g.:

<?php
// side effect: loads a file
include "file.php";

// declaration
function foo()
{
    // function body
}

But can you include a file inside a function?

e.g.

<?php
// declaration
function foo()
{
    // side effect: loads a file
    include "file.php";
}

Upvotes: 1

Views: 3839

Answers (3)

As the warning seems to hint at, you need to split the functionality into 2 parts. Let's say you start with some file, MyFunctionality.php, that causes this warning, like:

<?php

namespace MyFunctionality;

require_once 'vendor/autoload.php';

class MyClass
{
    public static function someFunction()
    {
    }
}

Put the class in it's own file, MyClass.php:

<?php

namespace MyFunctionality;

class MyClass
{
    public static function someFunction()
    {
    }
}

And then load that from the original file:

<?php

namespace MyFunctionality;

require_once 'vendor/autoload.php';

require_once 'MyClass.php';

Upvotes: 0

bdsl
bdsl

Reputation: 322

Yes, you may use include inside a function.

As far as this rule is concerned you can do anything you want as long as inside a function. Simply executing or including a file that declares a function doesn't run the code within the function. The function will only run if there is a call to it in some other file.

Include is mentioned in the rule just because it has the potential to cause side effects. This is no different to echo, sleep, or any other piece of code that has an observable effect when it runs.

Upvotes: 0

deceze
deceze

Reputation: 522005

The thing to understand about this rule is the difference between declaration and execution. You may also think about it as loading of code vs. execution of code. When you load code, you do not expect it to do anything just yet. E.g.:

require_once 'utility_functions.php';

You include this file because you want to use some utility function which is in that file. You need to include this file to use any function in it, you can't not include the file. However, if that file goes off and produces some side effects, just by you including it, you've just gotten into a deep rabbit hole. For example, say the file always changed your error reporting settings. That would be majorly annoying. You'd always have to reset your error reporting every time you included this file:

require_once 'utility_functions.php';
error_reporting(E_ALL);
ini_set('error_display', false);

That's obviously madness and a source of potential problems.

On the other hand, once you execute code, you expect that code execution to do something, possibly even to have side effects. And you can control code execution, unlike whatever the file does simply by being included.

require_once 'utility_functions.php';

utility_do_something(); // side effects here are OK and/or expected

Upvotes: 7

Related Questions