3

Same function name in different isolated classes is not allowed? What am I doing wrong?

I reduced my real code to the minimum required to make some test. Here it is:

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

class confFunctions {

    function getConf() {

        function doWork() {
            echo "I am from confFunctions!<br />";
        }

        doWork();
    }

}

class thePage {

    function loadPage() {

        function doWork() {
            echo "I am from thePage!<br />";
        }

        doWork();
    }

}

// Start check.
echo "Checking...<br />";

$conf = new confFunctions();
$conf->getConf();

$page = new thePage();
$page->loadPage();

?>

The output is:

Checking...
I am from confFunctions!
Fatal error: Cannot redeclare doWork() (previously declared in /var/www/Test2/index.php:11) in /var/www/Test2/index.php on line 23

Renaming one of the shared-name functions makes all working well. That is, changing doWork to doWork1 in the second class, like this:

class thePage {

    function loadPage() {

        function doWork1() {
            echo "I am from thePage!<br />";
        }

        doWork1();
    }

}

gives correct results:

Checking...
I am from confFunctions!
I am from thePage!

Should not what is inside a class be visible only to that class, if not declared public?

2
  • Set show_erros to on helps a lot. Commented Jun 17, 2013 at 17:35
  • 1
    if changing names by appending number is all you are doing to make it work, most likely you are using some restricted keyword as function name. Commented Jun 17, 2013 at 17:38

4 Answers 4

3

By declaring a function in a function, you are actually declaring the second function into the global scope.

If you want your functions to be limited to the class scope, don't declare a function in another function, but rather declare them under each other.

Consider this code that declares a function in another function (in a class):

<?php
class MyFunctions {
    function load() {
        function doWork() {
            echo "I am doing my work from global scope";
        }
    }
}

$mf = new MyFunctions();
$mf->load();
// $mf->doWork(); <-- won't work here

doWork(); // <-- this will work!
?>

Now consider this code that declares a function under another function (in a class).

<?php
class MyFunctions {
    function load() {
        //...
    }
    function doWork() {
        echo "I am doing my work from class scope";
    }
}

$mf = new MyFunctions();
// $mf->load(); <-- not really important anymore
$mf->doWork(); // <-- this will work now

// doWork(); <-- won't work here anymore
?>
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for letting me know that declaring a function in a function actually globalizes it. This is right what I needed to know to solve my problem.
@MaurizioDagradi No problem. Don't forget to mark the answer as accepted, so other can see the question needs no further attention. Use the check mark next to the answer, it will become green.
And finally, thank you to all answerers for their precious help!
Also, more information about Classes and Objects can be found in PHP's documentation regarding Classes and Objects. @MaurizioDagradi
1

Function scope is always namespace wide when declaring a named function.

You'll need to assign it to a variable to constrain it to a specific scope ($doWork = function() { }).

You seem to be going down an odd path though. Perhaps you should just use a private method?


Full example just to make it clear:

class confFunctions {

    function getConf() {

        $doWork = function() {
            echo "I am from confFunctions!<br />";
        };

        $doWork();
    }

}

class thePage {

    function loadPage() {

        $doWork = function() {
            echo "I am from thePage!<br />";
        };

        $doWork();
    }

}

Comments

1

I dont think you meant to nest the functions ? and your calling them from the global scope.

something like this is likely what you meant

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

class confFunctions {

    function getConf() {

        $this->doWork();
    }
    function doWork() {
        echo "I am from confFunctions!<br />";
    }

}

class thePage {

    function loadPage() {



        $this->doWork();
    }
    function doWork() {
        echo "I am from thePage!<br />";
    }

}

// Start check.
echo "Checking...<br />";

$conf = new confFunctions();
$conf->getConf();

$page = new thePage();
$page->loadPage();

?>

Comments

1

First guess would be that somehow you aren't properly closing your class from the first example. Different classes are definitely allowed to have the same function names, so there's something else going on in your code here that's not being shown through the psuedo-code you're posting.

UPDATE:

As NL-X said, by posting the function inside of a class function it then creates it in global scope. Thank you for updating your pseudo-code with actual examples.

3 Comments

More suitable as a comment as opposed to an answer, no?
Since the actual question in the OP is "Same function name in different isolated classes is not allowed?", I stand behind this as the "answer" to the question. I'll be happy to update my answer with more detail if we get more code to try and troubleshoot what the underlying problem is.
It seems to be that the OP posted with an inaccurate title (some kind of fixation as far as what the actual problem is)

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.