• I’m trying to use a function from one class and use it in another function in another class.
    classTest.php

    class Test extends WC_Payment_Gateway{
        public function testing(){
           $test = $this->testFunction();
        }
    }

    anotherClass.php

    class anotherClass{
        public function testFunction(){
            echo "This is the test function";
        }
    }

    I hope I made sense.

Viewing 8 replies - 1 through 8 (of 8 total)
  • You need to get an instance of the anotherClass and run the function off that. In your example, $this->testFunction() is trying to use the function in WC_Payment_Gateway.

    You want something like:

    class anotherClass{
        public function testFunction(){
            echo "This is the test function";
        }
    }
    
    class Test extends WC_Payment_Gateway{
        $anotherClass = new anotherClass;
        $anotherClass->testFunction();
    }

    Although whether that is specifically correct for you would depend on what exactly anotherClass is and does.

    Thread Starter cerberus478

    (@cerberus478)

    I get a
    Fatal error: Class ‘anotherClass’ not found

    Have you actually loaded the class anywhere? You need to make sure the file that holds you class is included somewhere.

    Thread Starter cerberus478

    (@cerberus478)

    Yep I have
    include(‘Test/src/anotherClass.php’);

    Thread Starter cerberus478

    (@cerberus478)

    I added
    include plugins_url( 'test/classes/Test/anotherClass.php' );

    but I’m getting this error
    Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in C:\wamp\www\wordpress\wp-content\plugins\test\classes\test.php on line 226

    Line 226 is where the include is.

    Your server (probably wisely) doesn’t support loading a PHP file with a URL. So don’t use plugins_url to get the location of the file, use plugin_dir_path() instead.

    Thread Starter cerberus478

    (@cerberus478)

    I’ve included the file, but I’m still getting the Class ‘anotherClass’ not found

    We’re getting into learning basic PHP here, so you probably want to go to somewhere like stackoverflow.com, but can you share all the code you’ve got so far?

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘How to get a function from one class into another function in another class’ is closed to new replies.