2

I want to call a function which is in another php class that I wrote in Wordpress. However I'm confused about the syntax, e.g. to get the header on a page you just call get_header(); etc. But how do i call a function in a specific class? For example in index.php I want to call a function named this_function() which reside in say test.php. What is the syntax for this?

I suspect it to be very simple, but I can't get the syntax right, and I can't any help online.

Thanks

2
  • 1
    Possible duplicate: stackoverflow.com/questions/5942224/… Commented Jun 25, 2012 at 16:03
  • aah. Thanks. That was exactly what I was looking for. Couldn't find it when I looked! Sorry about the duplicate. Commented Jun 25, 2012 at 16:04

1 Answer 1

1

First use require_once() to include the test.php, i.e.

require_once("path\test.php");

Then you can simply call this-function() and it will work.

You can use this as reference : http://www.php.net/manual/en/function.require.php

Sign up to request clarification or add additional context in comments.

3 Comments

I would recommend require_once() over include(), include_once(), or require(), because this method will make sure that the script you are looking for exists, and is only called once. Calling the same script more than once (usually happening by accident) can cause strange problems in your script.
But,upon failure, require produces a fatal E_COMPILE_ERROR level error, that will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.
True, however, if anything in your current script relies on variables/functions/classes/methods etc... within the included script, and it isn't there, you may have a whole list of errors that result from a missing script. Depending on the nature of these errors, they may or may not halt the compiler, which could result in PHP displaying errors and uncovering sensitive information about the composition of your script. In my opinion, this is more serious and even dangerous than one error saying that a script is missing.

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.