Skip to content
Advertisement

How can I call a function that is defined outside of a class from inside a class

So I haven’t really tried much, as I’m new to OOP, all I tried is to make a function for setting the progress bar for an action. When I call it from inside the class it says the function doesn’t exist. How can I get around this? Thanks!

If I don’t call the function in a class the progress bars works fine.

function progresSet($parama, $paramb) {
code to set prgress;

}

class register {

    function a(dasdsadasd) {
    
        progressSet('50', 'red');

    }

}

ERRORS

Fatal error: Uncaught Error: Call to undefined function progressSet() in /home/xkhckrzf/xenonmc/controller/controllers/register/main.php:280 Stack trace: #0 /home/xkhckrzf/xenonmc/controller/controllers/register/main.php(382): registerCntlr->main() #1 {main} thrown in /home/xkhckrzf/xenonmc/controller/controllers/register/main.php on line 280

Advertisement

Answer

You had some typos but sure you can, take a look:

<?php

function progressSet($parama, $paramb) {
    echo $parama . " - " . $paramb;
}

class register {
    function classFunction($param1, $param2) {
        progressSet($param1, $param2);
    }
}


$register = new register();
$register->classFunction('50', 'red')
?>
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement