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

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

Related

PHP Class instanced twice even with Singleton design pattern

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

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

Pass query result to another class

When I do a query in class A, I got all data already. class B need to use some of the data. I prefer pass part of the query result to B than do a new query in B. Class B will perform some jobs and the data will be changed in class B. How to pass the array $something_else to class B? Here is the classes:
class A{
public $something;
private $_project_obj;
function __construct( $id = null ){
if ( $id ) {
$this->id = $id;
$this->populate( $this->id );
}
}
function populate(){
$query = //do query
$this->somthing= $query['A'];
$this->something_else = $query['B'];
}
function save(){
// call save() in class B, $something_else is saved there
if ( $this->_project_obj instanceof B ) {
if ( true !== $this->_project_obj->save() ) {
return false;
}
}
// save $something and other stuffs in class A
// ......
}
function project() {
if ( !$this->_project_obj instanceof B ) {
if ( ( $this->id ) && ( loggedin_user_id() ) ) {
$this->_project_obj = new B( $this->id, loggedin_user_id() );
} else {
return false;
}
}
return $this->_project_obj
}
}
class B{
public $data_this;
public $data_that;
function __constructor( $id=null, $user_id=null){
if($id && $user_id){
return $this->populate();
}
return true;
}
function populate(){
$query = // do the same query as in class A
$something_else = $query['B'];
$this->data_this = $something_else['a'];
$this->data_that = $something_else['b'];
}
function save(){
// save all data as $something_else
}
function jobs(){
// perform jobs
}
}
It's not clear where in B you're needing something_else, so let's just add it as part of the constructor: Make the constructor function accept an additional parameter of something_else and save it to that class' property:
class B{
private var $_parent;
function __constructor( $parent, $id=null, $user_id=null){
$this->_parent = $parent; // Save reference to the "A" that contains this "B"
if($id && $user_id){
return $this->populate();
}
return true;
}
When A creates a B: $this->_project_obj = new B( $this, $this->id, loggedin_user_id() );
And when B needs to get the latest version of something_else from its parent A: $this->_parent->something_else
class class_b
{
public $something_else = NULL
}
$a = new class_a();
$b = new class_b();
$b->something_else = $a->something_else

php create class method at runtime

I am wondering if there is a way to attach a new method to a class at runtime, in php.
I mean, not on an instance level but directly to the class, so that all newly created instances, have this new method.
Can such a thing be done with reflection?
Thanks
Yes, you can.
Below is the way to create method in runtime in php 5.4.x.
The anonymous function is represented by Closure class started from 5.3.x. From 5.4.x, it add a Closure::bind static method to bind the anonymous function to a particular object or class.
Example:
class Foo {
private $methods = array();
public function addBar() {
$barFunc = function () {
var_dump($this->methods);
};
$this->methods['bar'] = \Closure::bind($barFunc, $this, get_class());
}
function __call($method, $args) {
if(is_callable($this->methods[$method]))
{
return call_user_func_array($this->methods[$method], $args);
}
}
}
$foo = new Foo;
$foo->addBar();
$foo->bar();
Did some playing around with whole thing. Seems that only thing you can potentially do with ReflectionClass is to replace an existing method. But even that would be indirectly.
I actually do not know any class-based language, where dynamic classes exist (then again, my knowledge is quite limited). I have seen it done only in prototype-based languages (javascript, ruby, smalltalk). Instead what you can do, in PHP 5.4, is to use Closure and add new methods to an existing object.
Here is a class which would let you perform such perversion to any object:
class Container
{
protected $target;
protected $className;
protected $methods = [];
public function __construct( $target )
{
$this->target = $target;
}
public function attach( $name, $method )
{
if ( !$this->className )
{
$this->className = get_class( $this->target );
}
$binded = Closure::bind( $method, $this->target, $this->className );
$this->methods[$name] = $binded;
}
public function __call( $name, $arguments )
{
if ( array_key_exists( $name, $this->methods ) )
{
return call_user_func_array( $this->methods[$name] , $arguments );
}
if ( method_exists( $this->target, $name ) )
{
return call_user_func_array(
array( $this->target, $name ),
$arguments
);
}
}
}
To use this, you have to provide constructor with an existing object. Here is small example of usage:
class Foo
{
private $bar = 'payload';
};
$foobar = new Foo;
// you initial object
$instance = new Container( $foobar );
$func = function ( $param )
{
return 'Get ' . $this->bar . ' and ' . $param;
};
$instance->attach('test', $func);
// setting up the whole thing
echo $instance->test('lorem ipsum');
// 'Get payload and lorem ipsum'
Not exactly what you want, but AFAIK this is as close you can get.
Have you taken a look at create_function() in the docs? You might also achieve the desired result by overloading.
This is possible with the runkit extension's runkit_method_add(). Be careful using this in production though.
Example:
<?php
class Example {}
$e = new Example();
runkit_method_add(
'Example',
'add',
'$num1, $num2',
'return $num1 + $num2;',
RUNKIT_ACC_PUBLIC
);
echo $e->add(12, 4);
You can use one of the below two methods also.
function method1()
{
echo "In method one.";
}
function method2()
{
echo "In method two.";
}
class DynamicClass
{
function __construct(){
$function_names = ['method1'];
foreach ($function_names as $function_name) {
if (function_exists($function_name)) {
$this->addMethod($function_name);
}
}
}
function addMethod($name)
{
$this->{$name} = Closure::fromCallable($name);
}
public function __call($name, $arguments)
{
return call_user_func($this->{$name}, $arguments);
}
}
$obj = new DynamicClass();
//Call method1 added in constructor
$obj->method1();
//Add method
$obj->addMethod('method2');
$obj->method2();

How to get the name of the calling class (in PHP)

define('anActionType', 1);
$actionTypes = array(anActionType => 'anActionType');
class core {
public $callbacks = array();
public $plugins = array();
public function __construct() {
$this->plugins[] = new admin();
$this->plugins[] = new client();
}
}
abstract class plugin {
public function registerCallback($callbackMethod, $onAction) {
if (!isset($this->callbacks[$onAction]))
$this->callbacks[$onAction] = array();
global $actionTypes;
echo "Calling $callbackMethod in $callbacksClass because we got {$actionTypes[$onAction]}" . PHP_EOL;
// How do I get $callbacksClass?
$this->callbacks[$onAction][] = $callbackMethod;
}
}
class admin extends plugin {
public function __construct() {
$this->registerCallback('onTiny', anActionType);
}
public function onTiny() { echo 'tinyAdmin'; }
}
class client extends plugin {
public function __construct() {
$this->registerCallback('onTiny', anActionType);
}
public function onTiny() { echo 'tinyClient'; }
}
$o = new core();
$callbacksClass should be admin or client. Or am I missing the point here completely and should go about this another way? It should be noted that I will only accept an answer that does not require me to send the classname as an argument to the registerCallback method.
If anyone came here looking for how to get the name of a calling class from another class like I did, check this out https://gist.github.com/1122679
EDIT: pasted code
function get_calling_class() {
//get the trace
$trace = debug_backtrace();
// Get the class that is asking for who awoke it
$class = $trace[1]['class'];
// +1 to i cos we have to account for calling this function
for ( $i=1; $i<count( $trace ); $i++ ) {
if ( isset( $trace[$i] ) ) // is it set?
if ( $class != $trace[$i]['class'] ) // is it a different class
return $trace[$i]['class'];
}
}
EG
class A {
function t() {
echo get_calling_class();
}
}
class B {
function x() {
$a = new A;
$a->t();
}
}
$b = new B;
$b->x(); // prints B
Use get_class():
$this->callbacks[$onAction][] = $callbackMethod;
$className = get_class($this);
// Call callback method
$className->$callbackMethod();
You should really do something like:
$this->registerCallback(array($this, 'onTiny'), anActionType);
That is how PHP works with handles to object methods.
From PHP 8+ you can use static::class rather than get_class($this).
This one is also auto-fixed with PHP Code Sniffer and rule SlevomatCodingStandard.Classes.ModernClassNameReference

Categories