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.
Related
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
(
)
in this below class i want to use class like with static methods and for use class methods without create new object from parent.
for example:
<?php
class Permission
{
protected $permission = false;
protected $id = 0;
public static function __construct()
{
return new static;
}
public function user( $id )
{
$this->id = $id;
}
public function check()
{
$this->permission = true;
}
public function item( $item )
{
return $item;
}
}
$bar = Permission::user(100)->item("HELLO");
print_r($bar);
this code not working and have problem. how to resolve this class problem?
That will not work because user method is not static, try changing this two methods, and this is good way of generating objects
public function __construct($id)
{
$this->id = $id;
}
public static function user( $id )
{
return new static($id);
}
I'd suggest you a singleton pattern, like this
class Permission
{
static protected $permission = false;
static protected $id = 0;
private static $_instance = null;
private function __construct () { }
public static function getInstance()
{
if (self::$_instance === null) {
self::$_instance = new self;
}
return self::$_instance;
}
public static function user( $userId )
{
self::$id = $userId;
return self::$_instance;
}
public static function check()
{
self::$permission = true;
return self::$_instance;
}
public static function item( $item )
{
return $item;
}
}
$bar = Permission::getInstance()->user(100)->item("HELLO");
print_r($bar);
You can chain methods in 'dynamic' classes by returning $this at the end of method (remember, you have a static).
class A {
public function someMethod()
{
// some code
return $this
}
public function otherMethod()
{
// some code
return $this
}
$a = new A();
$a->someMethod()->otherMethod();
}
I have an extended class with an overriden method doSomething().
For some reason the inherited class' method never runs only the base one.
class cDemoClass {
public static function getInstance() {
static $instance = null;
if ($instance === null)
$instance = new cDemoClass();
return $instance;
}
private function __construct() {
}
protected function doSomething() {
echo 'do something';
}
public function call_me() {
$this->doSomething();
}
}
class cDemoClassEx extends cDemoClass {
protected function doSomething() {
echo 'do something differently';
}
}
$baseclass = cDemoClass::getInstance();
$baseclass->call_me();
echo '<br/>';
$extendedclass = cDemoClassEx::getInstance();
$extendedclass->call_me();
result:
do something
do something
The second one should be "do something differently" at least that's what I'm expecting.
Can anyone tell me what I'm doing wrong? Thanks
In this case, you need using late static binding (5.3+). Change in parent method getInstance line :
$instance = new cDemoClass();
to
$instance = new static();
You will get:
do something
do something differently
Read more about this feature here: http://www.php.net/manual/en/language.oop5.late-static-bindings.php
Because cDemoClassEx::getInstance(); is still returning new cDemoClass();. You have to also overwrite the getInstance() method:
class cDemoClass {
public static function getInstance() {
static $instance = null;
if ($instance === null)
$instance = new cDemoClass();
return $instance;
}
private function __construct() {
}
protected function doSomething() {
echo 'do something';
}
public function call_me() {
$this->doSomething();
}
}
class cDemoClassEx extends cDemoClass {
public static function getInstance() {
static $instance = null;
if ($instance === null)
$instance = new cDemoClassEx();
return $instance;
}
private function __construct() {
}
protected function doSomething() {
echo 'do something differently';
}
}
$baseclass = cDemoClass::getInstance();
$baseclass->call_me();
echo '<br/>';
$extendedclass = cDemoClassEx::getInstance();
$extendedclass->call_me();
You have to override with the cDemoClassEx::getInstance() and change this line
$instance = new cDemoClass();
into
$instance = new cDemoClassEx();
You will also need to declare the cDemoClass::__construct() as protected or simply override it in cDemoClassEx.
There is a public library, and there is a class that can have only one instance in one PHP process, so it's Singleton. The problem is that initialization of this class require some configuration arguments and I can't find good issue to pass them in class constructor.
The only issue I found is:
public static function init($params) {
if(self::$instance) {
throw new Exception(__CLASS__ . ' already initialized');
}
$class = __CLASS__;
self::$instance = new $class($params);
}
public static function getInstance() {
if(!self::$instance) {
throw new Exception(__CLASS__ . ' is not initialized');
}
return self::$instance;
}
But I don't think that it's so really good.Is there any other ideas?
Thanks!
There is example of bad, but working issue:
if(!defined('PSEOUDSINGLETON_PARAM')) {
define('PSEOUDSINGLETON_PARAM', 'default value');
}
class PseoudoSingleton {
protected function __construct($param1 = PSEOUDSINGLETON_PARAM) {
// ...
}
public static function getInstance() {
if(!self::$instance) {
$class = __CLASS__;
self::$instance = new $class();
}
return self::$instance;
}
}
/* on library/utility level */
class nonSingletonService { public function __construct($options){} }
/* on application logic level, so, knows all context */
function getSingleton(){
static $inst;
if (!$inst) $inst=new nonSingletonService(calculateParameters());
return $inst;
}
Sample Singleton implementation:
class MySingleton
{
private static $_INSTANCE = null;
private function __construct()
{
// put initialization code here
}
public static function getInstance()
{
if(self::$_INSTANCE === null) self::$_INSTANCE = new MySingleton();
return self::$_INSTANCE;
}
}
Note that constructor is private, so it can only be called from the class itself.
References to the instance can be only obtained by getInstance call, which creates the object on first call, any subsequent calls will return references to an existing object.
Why are you checking it twice?
Just do the following:
private static function init($params) {
$class = __CLASS__;
self::$instance = new $class($params);
}
public static function getInstance($params) {
if(!self::$instance) {
self::init($params);
}
return self::$instance;
}
That way, you know that you only need to check once and you know you only call init() if the instance is not initialized.
i'm using DbSimple, but there is some code which i could write into another module. Here is it's code:
<?php
require_once 'config.php';
require_once 'DbSimple/Generic.php';
class MysqlWorker
{
private static $instance = NULL;
public function getInstance()
{
if( self::$instance == NULL )
{
self::$instance = new MysqlWorker();
}
return self::$instance;
}
private function __construct()
{
self::$instance = DbSimple_Generic::connect( 'mysql://'.MYSQL_USER.':'.MYSQL_PASS.'#'.MYSQL_HOST.'/'.MYSQL_DB.'' );
self::$instance->setErrorHandler( 'mysqlErrorHandler' );
self::$instance->query( "SET NAMES 'utf8'" );
}
private function mysqlErrorHandler( $message, $info )
{
if ( !error_reporting()) return;
echo "Database error: $message<br><pre>";
print_r($info);
echo "</pre>";
exit();
}
private function __clone() {}
}
?>
When i added code into class constructor:
var_dump( self::$instance );
I got:
object(DbSimple_Mysql)#2 (17) { ...}
So, there is everything is clear.
But when i'm using code in another location:
require_once 'modules/mysql_worker.php';
var_dump( MysqlWorker::getInstance() );
The result is:
object(MysqlWorker)#1 (0) { }
Why does MysqlWorker::getInstance object is empty?
Both the constructor and the static function getInstance assign something to the static property MysqlWorker::$instance.
class MysqlWorker
{
private static $instance = NULL;
private $connection = NULL;
public function getInstance()
{
if( self::$instance == NULL )
{
self::$instance = new MysqlWorker();
}
return self::$instance;
}
private function __construct()
{
$this->connection = DbSimple_Generic::connect( 'mysql://'.MYSQL_USER.':'.MYSQL_PASS.'#'.MYSQL_HOST.'/'.MYSQL_DB.'' );
$this->connection->setErrorHandler( array($this,'mysqlErrorHandler') );
$this->connection->query( "SET NAMES 'utf8'" );
}