|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-03-11 11:12 UTC] cybermerlin at ya dot ru
Description:
------------
1)
in __construct I wrote creator of methods hasSomeField for each field.
But if I tried to invoke it, then nothing happens.
2)
in __call I added writer to LOG name executing method, but in LOG exists only Exception for 'hasSomeField'.
Test script:
---------------
class Base{
protected $funcs = [];
public function __call($name, $args) {
error_log("\t>>>Call [$name](". json_encode($args). ") \r\n",3, LOGFILE);
if (method_exists($this, $name)) {
return call_user_func_array($this->$name, $args);
} else if (isset($this->funcs[ $name ])) {
return call_user_func_array($this->funcs[ $name ], $args);
} else {
throw new Exception("Method <$name> is not accessible.");
}
}
/**
* @param array $args
*
* @example new Base(["field1"=>"value1", "field2"=>"value2"])
* if u need override __constructor, then use:
* __construct($args){
* ..
* parent::__construct($args);
* }
*/
public function __construct($args = []) {
$nameClass = get_class($this);
#region auto create methods 'hasField' for everyone field\property (if there exists getter or setter)
$properties = get_class_vars($nameClass);
$keys = array_keys($properties);
foreach ($keys AS $k) {
if ((method_exists($this, "set" . ucfirst($k)) || method_exists($this, "get" . ucfirst($k)))
&& (!method_exists($this, "has" . ucfirst($k) && !isset($this->funcs["has" . ucfirst($k)])))
) {
$v = &$this->$k;
$this->funcs["has" . ucfirst($k)] = function () use (&$v) { return !empty($v); };
}
}
#endregion
foreach ($args as $k => $v) {
if (property_exists($nameClass, $k)) {
$this->$k = $v;
}
}
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 14 05:00:01 2025 UTC |
<?php abstract class Base{ protected $funcs = []; public function __call($name, $args) { error_log("\t>>>Call [$name](". json_encode($args). ") \r\n",3, LOGFILE); if (method_exists($this, $name)) { return call_user_func_array($this->$name, $args); } else if (isset($this->funcs[ $name ])) { return call_user_func_array($this->funcs[ $name ], $args); } else { throw new Exception("Method <$name> is not accessible."); } } /** * @param array $args * * @example new Base(["field1"=>"value1", "field2"=>"value2"]) * if u need override __constructor, then use: * __construct($args){ * .. * parent::__construct($args); * } */ public function __construct($args = []) { $nameClass = get_class($this); #region auto create methods 'hasField' for everyone field\property (if there exists getter or setter) $properties = get_class_vars($nameClass); $keys = array_keys($properties); foreach ($keys AS $k) { if ((method_exists($this, "set" . ucfirst($k)) || method_exists($this, "get" . ucfirst($k))) && (!method_exists($this, "has" . ucfirst($k) && !isset($this->funcs["has" . ucfirst($k)]))) ) { $v = &$this->$k; $this->funcs["has" . ucfirst($k)] = function () use (&$v) { return !empty($v); }; } } #endregion foreach ($args as $k => $v) { if (property_exists($nameClass, $k)) { $this->$k = $v; } } } } class Test extends Base{ private $field1; public function setField1($v){$this->field1 = $v;} public function getField1(){return $this->field1;} } $test = new Test(); $test->setField1(23423); echo $test->hasField1(); ?>