RobotLoader can't find a function

netteman
Member | 125
+
0
-

Hi,

I know that RobotLoader can find classes, but it seems it can't find functions. Is this the expected behavior?

Thanks :)


This is the file I used and it is located in the model directory:

<?php

//Nette says this function is undefined if I call it in a presenter
function x(){
    return 'x';
}

//I can use this class in a presenter without any problems
class Y{
    public $y = 'y';
}
Ondřej Kubíček
Member | 494
+
+2
-

it classic php autoloader, functions cannot be loaded, you have to add the file in which the function is, is loaded?

CZechBoY
Member | 3608
+
0
-

Is it in namespace?

netteman
Member | 125
+
0
-

CZechBoY wrote:

Is it in namespace?

The function and the class? No.

The presenter looks like this (I haven't managed to get x() working in the presenter)

<?php

namespace App\Presenters;

class HomepagePresenter extends BasePresenter
{
    public function renderDefault()
    {
        $y = new \Y();
        bdump($y);
    }
}

Last edited by netteman (2019-03-22 20:24)

CZechBoY
Member | 3608
+
0
-

and what about

<?php

namespace App\Presenters;

class HomepagePresenter extends BasePresenter
{
    public function renderDefault()
    {
        $y = \x(); // function call
        bdump($y);
    }
}

Last edited by CZechBoY (2019-03-22 21:55)

netteman
Member | 125
+
0
-

Throws exception:

  • Call to undefined function x(), did you mean _()?

The exception is thrown even when I use a longer name of the function

  • Call to undefined function getx()

(I tried deleting the cache)

It's not really a problem for me. I was playing around with Nette and I found this “RobotLoader finds classes, not functions” behavior and wanted to ask if it's normal or if I'm missing something :)

CZechBoY
Member | 3608
+
+2
-

I see, autoload is not triggered for functions.
You should use require_once directive on your own.

https://stackoverflow.com/…or-functions

Last edited by CZechBoY (2019-03-22 22:17)

Ondřej Kubíček
Member | 494
+
+1
-

that's what I wrote in my first post, probably a little complicated