PHP > LIBRARY > Overloading
 
PHP
Library
Tip&Tech
Q&A
 
PHP Function
phpschool
그누보드
LIBRARY
  HOME > PHP > LIBRARY
 
Overloading
작성일 : 19-01-08
조회 : 120  

mageic method 이용한 실제 사용 예시

<?php class Student{ private $_extraInfo = array(); public function __set($propertyName,$propertyValue){ $this->_extraInfo[$propertyName] = $propertyValue; } public function __get($propertyName){ if ( array_key_exists( $propertyName, $this->_extraInfo ) ) { return $this->_extraInfo[$propertyName]; } else { return null; } } public function __isset($propertyName){ if(isset($this->_extraInfo[$propertyName])){ echo "Property \$$propertyName is set.<br/>"; } else { echo "Property \$$propertyName is not set.<br/>"; } } public function __unset($propertyName){ unset($this->_extraInfo[$propertyName]); echo "\$$propertyName is unset <br/>"; } } $objStudent = new Student(); $objStudent->birthCountry = "Germany"; $objStudent->nationality = "United Kingdom"; echo "Overloaded Property name is \$birthCountry= "; echo $objStudent->birthCountry . "</br>"; echo "Overloaded Property name is \$nationality= "; echo $objStudent->nationality . "</br></br>"; isset($objStudent->birthCountry); isset($objStudent->nationality); echo "</br>"; unset($objStudent->birthCountry); unset($objStudent->nationality); echo "</br>"; isset($objStudent->birthCountry); isset($objStudent->nationality); ?>

Method Overloading

__call()

존재하지 않거나 접근 불가한 method를 호출할 때에 실행되는 함수

<?php class Test { public function __call($methodName, $arguments) { echo "Calling object method '$methodName' with Arguments(" . implode(', ', $arguments). ")</br></br>"; } } $obj = new Test; $obj->checkcall("check"); //Calling object method 'checkcall' with Arguments(check)

__callstatic

존재하지 않거나 접근 불가한 정적영역에서(static) method를 호출할 때에 실행되는 함수

<?php class Test { public function __call($methodName, $arguments) { echo "Calling object method '$methodName' with Arguments(" . implode(', ', $arguments). ")</br></br>"; } public static function __callStatic($methodName, $arguments) { echo "Static method $methodName is called."; if($methodName == "multiply"){ $total = 1; foreach($arguments as $num){ $total = $total * $num; } echo " answer is = $total"; }else{ echo " I am not doing anything Hun!!!."; } } } $obj = new Test; Test::Multiply(2, 4, 3, 4, 5); ?>

[출처] [php]Overloading|작성자 아아아


 
 

Total 77
번호 제   목 조회
공지 php 라이브러리 386
77 매직해시(MAGIC HASHES) 취약점 119
76 Trait 117
75    Trait 121
74 Overloading 124
73    Overloading 121
72 Type hinting/Type 설정하기/Throw error 120
71    Type hinting/Type 설정하기/Throw error 94
70    Type hinting/Type 설정하기/Throw error 118
69 object clone 127
68    object clone 114
67 serialize / unserialize 113
66 spl_autoload_register(오토로드) 103
65 window에서 composer깔기 114
64 Exception/try/throw/catch 123
63 Error의 종류 117
62    Error의 종류 118
61    Error의 종류 119
60 eval 119
59 Magic_methods 121
58    Magic_methods 127
 1  2  3  4  
 
개인홈페이지 덤벙닷컴은 프로그래머와 디자이너위한 IT커뮤니티 공간입니다.
Copyright ⓒ www.dumbung.com. All rights reserved.