I have created a bunch of functions for my wordpress theme.
I am thinking to put all function inside a class. So that I can call it using:
<?php $foo->function() ;?>
I want to know if I put my functions inside php class then will it cause any problem???
Please note I am using WordPress function inside my functions.
UPDATE
class foo{
function js($files){
$css = array($files);
foreach($css as $style){
wp_enqueue_script($style, get_template_directory_uri() . '/js/'.$style, array(), '', true );
}
}
}
$foo = new foo();
It works well when I call it directly:
$foo->js(‘bootstrap-transition.js,bootstrap-modal.js,bootstrap-dropdown.js’);
but when i call it insde another function like this:
function assets(){
$foo->js('bootstrap-transition.js,bootstrap-modal.js,bootstrap-dropdown.js');
}
then it shows Call to a member function on a non-object
error
Here is a basic primer about Object Oriented Programming in PHP for those that are not yet familiar with the concepts.
Objects are containers. They contain the definition functions and variables that represent a single abstract entity. This feature of Object Oriented Programming is called encapsulation. It means that a single capsule may contain the definition of multiple functions and variables that are meant to work together.
For instance, if you want to manipulate a product being sold in a e-commerce site, you can have an object that represents that product. It probably contains some variables that define the product name, description,price, etc.. It may also contain some functions to manipulate the object variables, store or retrieve the values of the product from a database, display the product details, etc..
In PHP objects are defined using classes. A class contains all the definitions of functions and variables that describe an object.
Here is a sample of simple product class:
You can create an object using the new operator. A PHP script can create multiple objects of the same class. You can have two distince products for sale in your e-commerce site defined by the same class.
For instance, if you have for sale the book A and the book B, you can use the same class to manipulate its name, description, price, etc.. Here follows an example of how to create PHP objects:
Classes allow you to manipulate information internally without having to use global variables to share information between different class functions.
For instance if you want to retrieve and display database query results, you can have a class with a function to execute the query and another function to display it. They use the class variables $results to store and retrieve the query results handle.
As you may see, you do not need to use any global functions nor any global variables to share information between the class functions. All the behavior of the products is encapsulated inside the Query class.
Object Oriented Programming provides other important features such as inheritance, which allows creating new classes that are extensions of existing classes. The extension classes may have more functions and variables. They may also redefine functions and variables already defined in the base class.
Resource Link for Object Oriented Programming with Classes
This is how I usually do it, for example, in
functions.php
(very simplified):Using this pattern you can organize all your functions inside a static class (doesn’t need to be instantiated) and then you can call functions in any template like so:
Hope this helps.
Yes, you can use classes in a WordPress theme. Just remember to instantiate an object from that class before calling its methods.
For instance, let’s assume we want to capsulate
function1
inside a class namedClass1
:Now in order to call the functions we need to instantiate an object from that class:
Given all this is done you can now call any functions defined in the class:
For more information and details check PHP’s documentation on Classes and Objects.