In PHP 5.3.6 I have a class with a method like this:
public function chunkText()
{
if(!function_exists('unloadChunkText')) {
function unloadChunkText() {
. . .
}
}
. . .
}
Where unloadChunkText is a helper method for chunkText. The problem is that whenever I call $obj->chunkText() I am given this error:
Cannot redeclare diagnostic\question\unloadChunkText() (previously
declared in
file.php:34)
in file.php
on line 34
Why isn’t function_exists telling me that this function already exists?
You are checking for the global function
unloadChunkText, instead of the namespace-specific functiondiagnostic\question\unloadChunkText. But I suspect your approach here is flawed.If you have a helper function for your method
chunkText(), define it in one of two ways:As a closure:
As a private method of the object:
Defining it as a private method probably makes more sense, so you don’t waste time redefining it every time you call
chunkText().