0

Using class inside a function giving error("Class Not Found");

When using outside function it works. For example -

require_once 'DoR/autoload.php';
use DoR\DoR;
use DoR\PSR\DoRx;
$dor = new DoR($agent);
$report = $dor->report();

When used within a function it gives error ("CLASS NOT FOUND"), For Example-

function runDOR($agent){
    require_once 'DoR/autoload.php';
    $dor = new DoR($agent);
    $report = $dor->report();
    return $report;
}

$report = runDOR();

Is their any alternative to having use satements for functions.

0

1 Answer 1

1

Disregarding the problematic design, you do not necessarily have to have use statements.

You can simply do $dor = new DoR\DoR($agent); (e.g. use the fully qualified class name).

Also, your first example most certainly does not work. You are importing two classes with the same name (DoR) without aliasing either. That won't work.

You've edited that problem out, but even then the use DoR\PSR\DoRx; statement is redundant and unnecessary. That class is not used anywhere in that snippet.

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

2 Comments

Edited the first Example(typo), $dor = new DoR\DoR($agent); works but what about the use DoR\PSR\DoRx;, how to load this class.
You are not using that class inside the function. You are using only DoR\DoR. And to use either without writing a use statement, just use the fully qualified name. Same with each case.

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.