How can a class method be accessed within a function in PHP?

To access a class method within a function in PHP, you can create an instance of the class within the function and then call the method on that instance. This allows you to use the class methods within the function scope.

class MyClass {
    public function myMethod() {
        return "Hello from myMethod!";
    }
}

function myFunction() {
    $myClass = new MyClass();
    return $myClass->myMethod();
}

echo myFunction(); // Output: Hello from myMethod!