I'm new to OOPs. I'm building a Plugin and I started with the "Wordpress Plugin Boilerplate with Namespace and AutoLoader Support."
I've even successfully added a section for a RESTful interface that I got working. However I have a a few functions that are common between Admin, the Front end and my REST interface. It would seem reasonable to put those common functions in the /inc/common
folder. But I can not get it working.
Perhaps it is clear to a more experienced OOPs programmer, but I have found no guidance on the web. How should these functions/menthods? be packaged in /inc/common
and how should they be called?
Plugin has folders:
/inc/admin -contains class admin
/inc/frontend -contains class frontend
/inc/libraries -contains class autoloader
/inc/core - contains class activator/deactivator/int/loader
/inc/common - empty folder
/inc/rest (I added)
I have tried just putting functions in a file in /int/common/common.php :
function common(){
do something
}
the including them from class admin/frontend/rest - that does not work. I have tried wrapping the functions in a class common:
class common{
public function common(){
do something
}
}
no luck there. I've gotten it so hosed up I had to restore from a working back up to got back to where I was last week so I load my code attempt. Some clue as to how to approach this is what I am looking for because clearly I'm missing something major here.
As best as I can recreate what I had:
Function in /inc/rest/class-rest
namespace PGC_Signoffs\Inc\Rest;
class Rest {
public function glider_club_update_signoff( \WP_REST_Request $request) {
include_once plugin_dir_path( __DIR__ ) . 'common/common.php';
$date_expire = $common_function($a,$b,$c);
}
}
Function in inc/common/common.php
public common_function( $a, $b, c){
// function to calculate the expire date.
return($start_date);
}
I'm new to OOPs. I'm building a Plugin and I started with the "Wordpress Plugin Boilerplate with Namespace and AutoLoader Support."
I've even successfully added a section for a RESTful interface that I got working. However I have a a few functions that are common between Admin, the Front end and my REST interface. It would seem reasonable to put those common functions in the /inc/common
folder. But I can not get it working.
Perhaps it is clear to a more experienced OOPs programmer, but I have found no guidance on the web. How should these functions/menthods? be packaged in /inc/common
and how should they be called?
Plugin has folders:
/inc/admin -contains class admin
/inc/frontend -contains class frontend
/inc/libraries -contains class autoloader
/inc/core - contains class activator/deactivator/int/loader
/inc/common - empty folder
/inc/rest (I added)
I have tried just putting functions in a file in /int/common/common.php :
function common(){
do something
}
the including them from class admin/frontend/rest - that does not work. I have tried wrapping the functions in a class common:
class common{
public function common(){
do something
}
}
no luck there. I've gotten it so hosed up I had to restore from a working back up to got back to where I was last week so I load my code attempt. Some clue as to how to approach this is what I am looking for because clearly I'm missing something major here.
As best as I can recreate what I had:
Function in /inc/rest/class-rest
namespace PGC_Signoffs\Inc\Rest;
class Rest {
public function glider_club_update_signoff( \WP_REST_Request $request) {
include_once plugin_dir_path( __DIR__ ) . 'common/common.php';
$date_expire = $common_function($a,$b,$c);
}
}
Function in inc/common/common.php
public common_function( $a, $b, c){
// function to calculate the expire date.
return($start_date);
}
That boilerplate uses autoloading, take advantage of it! I understand it's a bit overwhelming but with some training you will realize that autoloading and OOP are a superlative productivity boost.
Inside inc/common
create your class (for example, class-commonclass.php
)
<?php
namespace PGC_Signoffs\Inc\Common; \\declare the namespace for autoloading
class CommonClass {
public function commonFunction($a,$b,$c){ //could also be 'public static function'
//do your magic
return $start_date;
}
}
Note: the functions inside CommonClass
could be static, it depends on what you want to do with the class. If it's just a collection of functions go with static.
Then instance the class inside other classes, for example Admin
:
<?php
namespace PGC_Signoffs\Inc\Rest;
use PGC_Signoffs\Inc\Common\CommonClass; //declare you will be using CommonClass from the Inc\Common namespace
class Rest{
...
public function glider_club_update_signoff( \WP_REST_Request $request) {
$commonClass = new CommonClass();
$date_expire = $commonClass->commonFunction($a,$b,$c);
//or, if you went with a static function:
$date_expire = CommonClass::commonFunction($a,$b,$c);
}
}
Here's the gist
inc/common
being loaded at the moment? Are you loading them? Or do you expect that if you create a class in that folder it will just load auto-magically? Where do you create those objects? – Tom J Nowell ♦ Commented Feb 12, 2019 at 22:01$common_function($a,$b,$c)
is wrong, there is a$
too much before the function name. While this could work, I suggest you wrap that common_function inside a class and autoload it, look at my answer below – Fabrizio Mele Commented Feb 12, 2019 at 22:41