1

I have this class I created on a theme within a Wordpress environment.

class Theme {
    function __construct()
    {
       add_action('after_setup_theme', array(&$this, 'do_this'));
    }

    function do_this()
    {
       require_once('helper_functions.php');
    }
}
$theme = new Theme();

And within the helper_functions.php I have:

function get_image()
{
    return 'working';
}

But now I am puzzled because when I execute this

echo $theme->get_image();

It doesn't work....But if I called it directly it works like this:

echo get_image();

But I thought since I am using a class method, I need to use the class object to get to a class method...Why am I able to call it directly?

5 Answers5

0

Clearly, you're understanding it the wrong way. get_image() in this case, is not a method whatsoever. It's just another normal function that can be called during a script execution.

To make it a method, you have to declare it inside the class declaration.

class Theme 
{
    function __construct()
    {
       add_action('after_setup_theme', array(&$this, 'do_this'));
    }

    function do_this()
    {
       require_once('helper_functions.php');
    }

    function get_image()
    {
        return 'working';
    }
}

Read more about objects and classes here.

Kemal Fadillah
  • 9,760
  • 3
  • 45
  • 63
  • -ok perhaps you can help then. The reason why I am doing it this way is because I have a lot of functions to include as a method and I wanted to organize it out into separate files instead of declaring it all into a class which would make maintaining it hard. Is there a solution to what I want to achieve? Thanks. –  Apr 28 '12 at 16:15
0

The function get_image() is not a function set in the class Theme, it is set in a separate file, which is included in the class.

If you want it to be a function of the class then you need to write it in the class file, move the code into the class.

Alternative you could make use of class extends

class Helper_functions  {

    public function get_image() {
        return "working!";
    }
}

And change the Theme class file to

class Theme extends Helper_functions {

    function __construct()
    {
       add_action('after_setup_theme', array(&$this, 'get_image'));
    }

}

$theme = new Theme();

Alternative
Since you said it was several functions in several files you can do this, either in the Theme class file or in an extended class file.

class Theme {

    ...

    function get_image() { include('theme_file_get_image.php'); }
    function another_function { include('theme_file_another_function.php'); }
}
fhugas
  • 392
  • 1
  • 10
  • The reason why I am doing it this way is because I have a lot of functions to include as a method and I wanted to organize it out into separate files instead of declaring it all into a class which would make maintaining it hard. Is there a solution to what I want to achieve? Thanks. –  Apr 28 '12 at 16:26
  • If you really need the functions to be a part of the class then you have to define them in the class. If you absolutely want several files to manage then check out the alternative answer I have provided :) – fhugas Apr 28 '12 at 16:52
  • Thanks for that however, I thought that is exactly what I did...I just used require_once instead of include...is there a difference? –  Apr 28 '12 at 16:53
  • Oh, you have to remove the `function get_image() { }` Leave only the code you want to execute. Then it should work just fine. – fhugas Apr 28 '12 at 17:00
  • Or does `helper_functions.php` contain a lot of functions? – fhugas Apr 28 '12 at 17:01
  • yeah, I have separate files for example database.php, pagination.php, utils.php. So I didn't want to copy all the functions from those files and put it directly into the theme class because that would be a mess and hard to maintain. That is why I created the helper_functions method within the class and include the other files I needed...That's how I got up to this point. Perhaps with that, you can see what I am trying to do? –  Apr 28 '12 at 17:07
  • Ok, so the file `helper_functions.php` does not solely contain functions for this class then? I rewrote my answer a little, this way you can put the functions in the class `Helper_functions` to keep the Theme class more tidy. – fhugas Apr 28 '12 at 17:12
  • no these don't work. i just want 1 class served mostly just for encapsulation to prevent namespace conflicts...I gave up...I will stick with procedural code instead as classes are difficult to use in Wordpress environment for themes...But your original answer does answer my concept question so i will mark that as the correct one... –  Apr 28 '12 at 19:22
0

An answer in this question sums up the result of what you're seeing here:

"The code within the included file is executed in the same scope as the function (with defined functions and classes being global), not inserted into it, replacing what else is there."

So the function within the included file is defined in the global scope, which is why the call to get_image() works.

This should be equivalent to:

//Include the helper functions
require_once('helper_functions.php');

class Theme 
{
    function __construct()
    {
       add_action('after_setup_theme', array(&$this, 'do_this'));
    }

    function do_this()
    {
       get_image();
    }
}

$test = new Theme();
echo $test->do_this(); //'working';

Note that get_image() is in the global score and not a member function of the Theme class.

Community
  • 1
  • 1
Will Demaine
  • 1,516
  • 1
  • 11
  • 12
0

include and require are often said to be "like copy/pasting" the file contents into the parent script, and that seems to be what you're anticipating. However, it's not really correct, and that's why what you're doing doesn't work.

some languages have compiler macros that do text replacement before interpreting the code, and so that would wpork. php doesnt though, and all include statements are evaluated at runtime. in your case, the class is fully defined before that require statement gets executed. as a result, you're just executing the code, which defines a global function.

goat
  • 31,486
  • 7
  • 73
  • 96
0

You cannot split class definitions. All of it needs to go in one go. If your class is too big, maybe it has too many responsibilities.

  • Separate business logic from factory logic (factory logic is the logic which instansiates new objects, business logic are those objects).
  • Separate classes by function.
  • Use inheritance and polymorphism.

When you include a file inside of a function, it only exist in the scope of that function. Meaning what you did is the equivalent to:

public function do_this() {
    function get_image() { ... }
}

Which doesn't really do anything.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308