Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: method with same name in different classes not working

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?

like image 718
Maurizio Dagradi Avatar asked Dec 06 '25 06:12

Maurizio Dagradi


2 Answers

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
?>
like image 104
nl-x Avatar answered Dec 08 '25 19:12

nl-x


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();
    }

}
like image 37
Corbin Avatar answered Dec 08 '25 19:12

Corbin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!