O
O
Optimus2015-09-26 14:33:35
PHP
Optimus, 2015-09-26 14:33:35

What is the best way to avoid the redeclare error?

The function is called in a loop.
A class is declared inside the function, as a result, the error "Cannot redeclare class"
If I take the class out of the function, will it stop seeing it? Does the function see variables either only accepted or declared inside it? How to properly design such a case? Convert the class to a function too?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nazar Mokrinsky, 2015-09-26
@nazarpc

You are doing some nonsense.
According to a normal class file, apart from the class itself, nothing else should contain.
Next, you autoload classes according to PSR-4 and forget about Cannot redeclare class for the rest of your life.

S
Sergey, 2015-09-26
Protko @Fesor

A class is declared inside the function,

I'm sorry, what?
Something? In PHP, everything, functions, classes, etc. is registered in the global scope.
classes and functions are not variables.
do you mean object? If so then yes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question