PHPã§åçç¶æ¿
Rubyã«ããç¹ç°ã¯ã©ã¹ãç¹ç°ã¡ã½ããã®ä¾¿å©ãããPHPã§ãå®ç¾ãã¦ã¿ããã¨è©¦ã¿ã¾ãããRubyã®ç¹ç°ã¯ã©ã¹ã¯ããã§ã«ã¤ã³ã¹ã¿ã³ã¹åããããã¯ã©ã¹ä¸æãªãªãã¸ã§ã¯ãã«ããªã¼ãã¼ã©ã¤ãã§ã¡ã½ããã追å ãããã®ã§ãã
ç¹ç°ã¯ã©ã¹ã¯ã¹ã¯ãªããè¨èªãªãã§ã¯ã®æ©è½ã§ãããã¨ãã°ããã¬ã¼ã ã¯ã¼ã¯ã®æ©ã段éã§ã¤ã³ã¹ã¿ã³ã¹åããããªãã¸ã§ã¯ãããå¾åã®ã¦ã¼ã¶ã³ã¼ãã«é©åããªãå ´åãèãã¾ãããããã«ã®ã©ããã¼ã¯ã©ã¹ãå®ç¾©ãã¦ã¢ããã¿ã¼ãã¿ã¼ã³ãé©ç¨ãã¦ãã¾ãããã§ãããç¹ç°ã¯ã©ã¹ã§ãªãã¸ã§ã¯ãèªä½ã®ã¡ã½ããã追å ã»ä¸æ¸ãããã°ãã¤ã³ã¿ã¼ãã§ã¼ã¹ãåããããã¨ãã§ããé¢åãªã©ããã¼ãè¦ãã¾ããã以éãæ¡å¼µæ¸ã¿ãªãã¸ã§ã¯ãã¯ãªãªã¸ãã«ãªãã¸ã§ã¯ãã®ããã«ä½¿ãã¾ãã
Javascriptã§ãPythonã§ããç¸å½ã®ãã¨ã¯ç°¡åã«ã§ãã¾ãã
var obj = new Object(); obj.extraMethod = function(){ ...; } obj.extraMethod();
obj = object()
obj.extraMethod = lambda self:...
obj.extraMethod()
ãã¦ãæååããã¡ãã¡ãã¼ã¹ãã¦ããããã«ãè¨èªä»æ§ã¯JavaãC++ã«è¿ãã¨ããå¤ãªè¨èªãPHPã§ã¯ã©ãã§ãããï¼
ç¾å¨ã®PHPã«ã¯ãç¹æ®ãªã¡ã½ããåãããã¤ããããå¤æ°ãã¡ã½ããããªãã£ãå ´åã®ãã§ã¤ã«ã»ã¼ããèæ
®ããã¦ãã¾ãã
http://php.net/manual/ja/language.oop5.overloading.php
ãã®ç¹æ®ã¡ã½ããåã使ã£ã¦ãæ¡å¼µåã®ãªãã¸ã§ã¯ãã¤ã³ã¹ã¿ã³ã¹ãã¿ã¼ã²ããã¨ãã¦ãå¤æ°æä½ãã¡ã½ããå¼ã³åºãããã¹ã¦å§è²ãããã®ããã§ãç¹ç°ã¡ã½ããåãããã¡ã½ãããèªåã®ã¯ã©ã¹ã«å®ç¾©ããã¨ããã¤ãããã¯ãªãªãã¸ã§ã¯ãå¤å½¢ãã§ãããã§ãã
<?php /** * å§è²ã®åºæ¬ãªãã¸ã§ã¯ã */ class DelegateBase { private $_parent; public __construct($parent) { $this->_parent = $parent; } public function __get($name) { return $this->_parent->$name; } public function __set($name, $value) { $this->_parent->$name=$value; } public function __isset($name) { return isset($this->_parent->$name); } public function __unset($name) { unset($this->_parent->$name); } function __call($name, $args) { return call_user_func_array(array($this->_parent, $name), $args); } } // Mix-inã¯ãªãã®ã§ãåç´ã«ç¶æ¿ãã¦ä½¿ããã⦠class SpecialClass extends DelegateBase { function foo() { return "extended " . $this->_parent->foo(); } function baz() { return $this->bar() . " via baz"; // barã¯_parentã«å§è² } } // $existing_obj ã¯ã¡ã½ãã foo 㨠bar ãæã¤æ¢åã®ã¤ã³ã¹ã¿ã³ã¹ $myobj = new SpecialClass($existing_obj); echo $existing_obj->foo(); // "foo" echo $existing_obj->bar(); // "bar" echo $existing_obj->baz(); // ã¨ã©ã¼ echo $myobj->foo(); // "extended foo" echo $myobj->bar(); // "bar" echo $myobj->baz(); // "bar via baz" //ãã®ä»ã$existing_obj ãæã£ã¦ããã¡ã½ããã¨å¤æ°ã¯ãã¹ã¦ã$myobj ãããã®ã¾ã¾ä½¿ãã ?>
ãããªæãã«ãªãã¾ãããããèªä½ã®åä½ã確èªãã¦ã¯ãã¾ããããç価ãªå®è£ ã§ãã¾ãåãã¦ãã®ã§ããã¶ã大ä¸å¤«ãããã¡ã¨ãã¦ã¯ãèªåçãªã©ããã¼ã«ãªã£ãããã§ãããããããç®çã¯éæã§ãã¾ããã
ããã§ãæ¬ä¼¼æ´¾çã¯ã©ã¹ã®ã»ã(SpecialClass)ã®å®è£ ã³ã¼ãããã¼ã¹ããã®ããé 延ããããã¨ãã§ãã¾ããããããããæ´¾çã®å¿ è¦ããªããããããªãã®ã§ãéçæ´¾çã®ããã«ãæ¯åæ´¾çã¯ã©ã¹ããã¼ã¹ãã¦è² è·ããããã®ã¯ã°ãã°ãããã§ãã
ã§ãè¦æ³¨æ!! å¼ã³åºãããã£ããåºåºãªãã¸ã§ã¯ãã«å ¥ã£ã¦ãã¾ãã¨ããããæ´¾çå´ã®ã¯ã©ã¹ã§ãªã¼ãã¼ã©ã¤ããã¦ãã¦ããåºåºãªãã¸ã§ã¯ãã®ã¡ã½ãããå¼ã°ãã¦ãã¾ãã¾ããæ£ãããç¶æ¿ãã§ã¯ããã¾ãããã¨ããæå³ã§ããããªã²ã¼ããã¨åã¥ãã¦ãã¾ãã
ãã£ã±ããåçãªã¹ã¯ãªããè¨èªã«ãéçãªã¯ã©ã¹ç¶æ¿ã¯ä¼¼åãã¾ãããã
ããããæ´¾çãããå¤ããªãã£ããå§è²å ãåå²ãããããªã©ãã¦ãããã¯ã¹ã¤ã³ãã©ããä½ããã¨ãã§ãããã§ããã¾ãããã£ããå§è²ãã¦ãã¾ãã¨å¸°ã£ã¦ããªãããããªãã¸ã§ã¯ããæ··ãã¦ããããããããå ¥ãå£ãåãã¤ã³ã¹ã¿ã³ã¹ã ãã¨ãããããã®æå³ã«ãããªããªãã§ããããã©ã