PHP Class instanced twice even with Singleton design pattern - php

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
}
}
}

Related

PHP How to self declare by parent function in child class?

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
(
)

PHP - How can I check a class has no arguments constructor?

In few worlds : I would like to do the same but in PHP.
In details : I have a Class A method that instantiates a Class X which can be a Class B or C.
Class A
class A{
...
protected function init(){
if( !empty( $this->sub_pages ) ){
foreach ( $this->sub_pages as $sub_page ){
$class = $sub_page['class_path'];
//Here, I need to check if ClassX ( = $class) constructor has arguments.
if( no arguments){
new $class();
}else{
new $class( $sub_page['data'] );
}
}
}
}
...
}
Class B
class B{
public function __construct(){ //<-- No arguments
}
}
Class C
class C extends D{
public function __construct( $data ){ //<-- With arguments
parent::__construct( $data );
}
}
Someone know the answer ?
If you want to check whether a class has a constructor and if that constructor accepts any params or not then you can do it using PHP's Reflection Class for example:
$reflector = new \ReflectionClass('SomeClass');
$constructor = $reflector->getConstructor();
if ($constructor && $constructor->getParameters()) {
// Since your class needs $sub_page['data'] and
// you already have this in your current scope
$instance = $reflector->newInstanceArgs($sub_page['data']);
} else {
$instance = new SomeClass;
}
Btw, If you have type hinted dependencies (like other class instance) then you can find out what is the dependency and can also new up that dependent class to pass as param.
Base on #The Alpha answer, I found this :
class A{
...
protected function init(){
if( !empty( $this->sub_pages ) ){
foreach ( $this->sub_pages as $sub_page ){
try {
$reflector = new \ReflectionClass( $class );
if (!$constructor = $reflector->getConstructor()) {
printf( "The Class '%s' has not got a constructor", $class );
}else {
// has a constructor
if ( $paramsArray = $constructor->getParameters() ) {
new $class( $sub_page['data'] );
}else{
new $class();
}
}
} catch ( \ReflectionException $e ) {
echo $e;
}
}
}
...
}

Class not found in Php when trying to extends a class

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.

Call parent function inside included/required file

I would like to know if it is possible to call a function of a parent file inside a included file and how that could be work.
For an example we got that:
parent_file.php :
<?php
if ( ! class_exists( 'Parent_Class' ) ) {
class Parent_Class {
public $id = 10;
public static function getInstance() {
if ( ! ( self::$_instance instanceof self ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
public function init() {
include 'child-file.php';
$child = new Child_Class($id);
$child->action();
}
public function edit($values_of_id) {
return $values_of_id;
}
?>
child_file.php :
<?php
if ( ! class_exists( 'Child_Class' ) ) {
class Child_Class {
private $id;
function __construct(){
$params = func_get_args();
if(!empty($params))
foreach($params[0] as $key => $param)
if(property_exists($this, $key))
$this->{$key} = $param;
parent::__construct( array(
'id' => $this->id,
) );
}
public function action() {
$url = 'http://myserver.com/edit_child.php?page='. $_REQUEST['page'] .'&action=select&id='. absint($this->id) ) );
$action = '<a href='. $url .'>Edit</a>'
return $action;
}
public function select_table_row() {
if ( isset( $_GET['action'] ) && !empty( $_GET['action'] ) )
$row = $_GET['id'];
$connection = new mysqli($servername, $username, $password, $dbname); // fictitious params
$query = "SELECT * FROM MyTable WHERE id = $row";
$values_of_id = mysqli_query($connection, $query);
// Call function of parent_file.php
edit($values_of_id);
}
$this->select_table_row();
?>
This is a fictitious example and I know that the code couldn't work like this. I just want to aim to my question and make my thoughts visual and maybe more comprehensible.
Important is that I cannot include parent_file.php in my child_file.php because the Child_Class could be access from multiple files.
I'm sorry if this question was already asked. I'm limited in my buzzwords for this topic and couldn't find anything like this.
You have to pass the parent class object to the child class, something like this:
class parentClass {
private $str;
public function __construct($str){
$this->str = $str;
}
public function getChild() {
$obj = new childClass($this);
$obj->callParent("send");
}
public function send() {
echo $this->str;
}
}
class childClass {
private $parent;
public function __construct($parent) {
$this->parent = $parent;
}
public function callParent($method) {
return $this->parent->$method();
}
}
$obj = new parentClass("hello");
$obj->getChild(); // prints "hello"
Demo: https://eval.in/403427

Singletons and DbSimple

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'" );
}

Categories