I basically have two classes namely A and MOC.
When i call HCAA()->get_moc() i don't get the class but instead i get null. Why is this happening ?
final class A {
private $moc;
private function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
public static function instance() {
static $instance;
if( ! isset($instance) ) {
$instance = new A();
}
return $instance;
}
public function init() {
if ( is_admin() && current_user_can('manage_options') ) {
$this->plugin_start();
}
}
private function plugin_start(){
$this->moc = new MOC();
var_dump($this->moc);
}
public function get_moc(){
return $this->moc;
}
}
class MOC{
private $moc;
public function __construct() {
add_action( 'customize_register', array($this, 'customize_register'), 12 );
}
public function customize_register( $wp_customize ) {
$this->moc = $wp_customize;
}
public function add( $options ) {
var_dump('add called');
}
}
function HCAA() {
return A::instance();
}
i am initially calling the HCAA function like this.
HCAA();
Plus i am also getting this error.
PHP Fatal error: Call to a member function add() on a non-object in /app/public/wp-content/plugins/hc-moc/utils/test.php on line 47
Related
Class:
if( ! class_exists('MY_CLASS') ) :
class MY_CLASS {
private static $_instance = null;
private static $counter = 0;
private function __construct() {
self::$counter++;
// Do stuff here.
echo "instances: " . self::$counter . "<br>";
}
// Other functions here
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new MY_CLASS();
}
return self::$_instance;
}
}
function ctp() {
return MY_CLASS::instance();
}
// initialize
ctp();
endif; // class_exists check
The $counter is always 2. I've checked and the function instance() enters the if condition is_null( self::$_instance ) twice.
Really not being able to make this class only be instanced once. Please help.
Sorry, found what was causing the problem.
So in My_Class I had:
function includes() {
require_once( 'path-to-second-class.php' );
// several other requires
}
private function init() {
// various class instantiations « new Class_Name() », but not for Second_Class
}
And in second-class.php I had
class Second_Class {
$taxs = array();
function __construct() {
$this->taxs = ctp()->get_ctp_taxs(); // which returns Main_Class->$taxs
// do other stuff
}
function do_stuff() {
foreach( $this->taxs as $tax_name => $tax_object ) {
// do stuff
}
}
}
new Second_Class();
This, for some reason that I tbh don't know, doesn't work so I changed it to:
My_Class:
function includes() {
require_once( 'path-to-second-class.php' );
// several other requires
}
private function init() {
// same other instantiations
new Second_Class();
}
And in second-class.php I now have:
class Second_Class {
function __construct() {
// do same other stuff
}
function do_stuff() {
foreach( ctp()->get_ctp_taxs() as $tax_name => $tax_object ) {
// do stuff
}
}
}
Parent
class MWCEMod {
private static $mod;
public function get_mod(){
var_dump( self );
if ( null == self::$mod )
self::$mod = new MWCEMod();
return self::$mod;
}
private function __construct(){
var_dump('something1');
}
}
Child
class MWCEMod_child extends MWCEMod{
private function __construct(){
var_dump('something2');
}
}
add_action( 'MWCE_Load_Mods', array('MWCEMod_acf','get_mod') );
I would like to to change above code so MWCEMod_child::get_mod() would call self::$mod = new MWCEMod_child(); not self::$mod = new MWCEMod();. Is there any way for doing it?
I'm trying to learn oop PHP on Making Wordpress Plugin. Those are modules for main class and i load them this way:
class MonWayContentEditor {
private static $instance;
private $options;
public $plugin_url,
$plugin_dir;
public static function get_instance() {
if( get_current_user_id() == 2 ){
if ( self::$instance == null )
self::$instance = new MonWayContentEditor();
return self::$instance;
}
return null;
}
private function __construct(){
$this->options = array(
);
$this->plugin_url = plugins_url().'/monway-editor/';
$this->plugin_dir = plugin_dir_path( __FILE__ );
$this->load_modules();
do_action( 'MWCE_Load_Mods');
}
private function load_modules(){
include($this->plugin_dir.'/mods/MWCEMod.php');
if(class_exists('acf'))
include($this->plugin_dir.'/mods/acf.php');
}
}
add_action( 'plugins_loaded', array( 'MonWayContentEditor', 'get_instance' ) );
I dont know how to explain it better.
The main thing seems to be to create an instance of the derived class as opposed to the base class. I've altered the code to allow me to show the principle of using get_called_class() and then using this name to create the object.
class MWCEMod {
private static $mod;
public function get_mod(){
if ( null == self::$mod )
$className = get_called_class();
self::$mod = new $className();
return self::$mod;
}
private function __construct(){
var_dump('something1');
}
}
class MWCEMod_child extends MWCEMod{
public function __construct(){
var_dump('something2');
}
}
$n = new MWCEMod_child();
print_r($n->get_mod());
This code outputs...
.../Test/t1.php:21:
string(10) "something2"
.../Test/t1.php:21:
string(10) "something2"
MWCEMod_child Object
(
)
I am trying to set a variable of $tester that can be used in multiple functions in MyClass.
I have set the variable and added a function on __construct() but I am getting an undefined variable notice when I try to echo it out - why is this?
class MyClass {
public $tester;
public function __construct() {
add_action( 'init', array( &$this, 'variables' ) );
add_action( 'init', array( &$this, 'do_stuff' ) );
}
public function variables() {
$tester = get_option( 'an_option' );
}
public function do_stuff() {
echo $tester;
}
}
$my_class = new MyClass();
class MyClass {
public $tester;
public function __construct() {
add_action( 'init', array( &$this, 'variables' ) );
add_action( 'init', array( &$this, 'do_stuff' ) );
}
public function variables() {
$this->tester = get_option( 'an_option' );
}
public function do_stuff() {
echo $this->tester;
}
}
$my_class = new MyClass();
Try this. Properties in a class called always with $this->.
Have a look on this documentation
I am writing a plugin where i have to extends a class from another php file. But i am getting Fatal error: Class 'EWWL_Admin_Init' not found.
Here is my main file code.
class Eden_Woocommerce_WL_Admin {
private static $instance = null;
public static function getInstance() {
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
public function __construct() {
$this->includes();
$this->add_menu_page();
}
private function includes() {
include_once( EWCWL_PLUGIN_DIR . '/includes/admin/ewwl-admin-menu.php' );
include_once( EWCWL_PLUGIN_DIR . '/includes/admin/ewwl-admin-init.php' );
}
private function add_menu_page() {
EWWL_Admin_Init::getInstance();
EWCWL_Admin_menu::getInstance();
}
}
here is EWCWL_Admin_menu class
class EWCWL_Admin_menu extends EWWL_Admin_Init {
private static $instance = null;
public static function getInstance() {
if(is_null(self::$instance)){
self::$instance = new self();
}
}
public function __construct($args = array()) {
if (!empty($args)) {
$this->settings = $args;
if (isset($this->settings['create_menu_page']) && $this->settings['create_menu_page']) {
add_action('admin_menu', array($this, 'add_ewwl_menu_page'));
}
}
}
public function add_ewwl_menu_page() {
$position = apply_filters('ewcwl_plugins_menu_item_position', '62.32');
add_menu_page('plugin_panel', __('Menu title', 'ed-wcwl'), 'manage_options', 'ed_plugin_panel', NULL, '', $position);
}
}
Here is EWWL_Admin_Init class
class EWWL_Admin_Init {
private static $instance = null;
public static function getInstance() {
if( is_null(self::$instance) ){
self::$instance = new self();
}
return self::$instance;
}
public function __construct() {
}
}
May be i am missing something. Thanks in advance.
It happened because EWWL_Admin_Init didn't included before EWCWL_Admin_menu. Swapping two lines
include_once( EWCWL_PLUGIN_DIR . '/includes/admin/ewwl-admin-init.php' );
include_once( EWCWL_PLUGIN_DIR . '/includes/admin/ewwl-admin-menu.php' );
from Eden_Woocommerce_WL_Admin class solved the issue.
I would like to break apart the function used in Class to make it easier to understand, however I cant seem to work out how to pass variables across between two or more functions while maintaining the WP INIT hook. I have simplified my code for this question.
I am not sure if using multiple add_action in __construct is the correct way to go about it.
Thanks.
//called from template/index.php
do_action('foo');
//in template/fuctions.php
add_action( 'init', array ( 'foo', 'init' ) );
class foo
{
public $stillnotworking;
public static function init()
{
new self;
}
public function __construct()
{
add_action( 'foo', array ( $this, 'part1' ) );
add_action( 'foo', array ( $this, 'part2' ) );
}
public function part1()
{
$this->x = '123';
$stillnotworking = '123';
}
public function part2()
{
echo $this->x; //not working
echo $stillnotworking;
}
public function __toString()
{
echo $this->x; //not working
echo $stillnotworking;
}
function __destruct() {
}
}
You haven't declare your $x within you class. Try
add_action( 'init', array ( 'foo', 'init' ) );
class foo
{
public $x;
public $stillnotworking;
public static function init()
{
new self;
}
public function __construct()
{
add_action( 'foo', array ( $this, 'part1' ) );
add_action( 'foo', array ( $this, 'part2' ) );
}
public function part1()
{
$this->x = '123';
$stillnotworking = '123';
}
public function part2()
{
echo $this->x; //not working
echo $stillnotworking;
}
public function __toString()
{
echo $this->x; //not working
echo $stillnotworking;
}
function __destruct() {
}
}