Advertisement
Guest User

Untitled

a guest
Sep 16th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.50 KB | None | 0 0
  1. function myFunc() {
  2. for ($i = 0; $i < func_num_args(); $i++) {
  3. printf("Argument %d: %sn", $i, func_get_arg($i));
  4. }
  5. }
  6.  
  7. /*
  8. Argument 0: a
  9. Argument 1: 2
  10. Argument 2: 3.5
  11. */
  12. myFunc('a', 2, 3.5);
  13.  
  14. class MyClass {
  15. public function __call($name, $args) {
  16.  
  17. switch ($name) {
  18. case 'funcOne':
  19. switch (count($args)) {
  20. case 1:
  21. return call_user_func_array(array($this, 'funcOneWithOneArg'), $args);
  22. case 3:
  23. return call_user_func_array(array($this, 'funcOneWithThreeArgs'), $args);
  24. }
  25. case 'anotherFunc':
  26. switch (count($args)) {
  27. case 0:
  28. return $this->anotherFuncWithNoArgs();
  29. case 5:
  30. return call_user_func_array(array($this, 'anotherFuncWithMoreArgs'), $args);
  31. }
  32. }
  33. }
  34.  
  35. protected function funcOneWithOneArg($a) {
  36.  
  37. }
  38.  
  39. protected function funcOneWithThreeArgs($a, $b, $c) {
  40.  
  41. }
  42.  
  43. protected function anotherFuncWithNoArgs() {
  44.  
  45. }
  46.  
  47. protected function anotherFuncWithMoreArgs($a, $b, $c, $d, $e) {
  48.  
  49. }
  50.  
  51. }
  52.  
  53. class ParentClass
  54. {
  55. function mymethod($arg1 = null, $arg2 = null, $arg3 = null)
  56. {
  57. if( $arg1 == null && $arg2 == null && $arg3 == null ){
  58. return 'function has got zero parameters <br />';
  59. }
  60. else
  61. {
  62. $str = '';
  63. if( $arg1 != null )
  64. $str .= "arg1 = ".$arg1." <br />";
  65.  
  66. if( $arg2 != null )
  67. $str .= "arg2 = ".$arg2." <br />";
  68.  
  69. if( $arg3 != null )
  70. $str .= "arg3 = ".$arg3." <br />";
  71.  
  72. return $str;
  73. }
  74. }
  75. }
  76.  
  77. // and call it in order given below ...
  78.  
  79. $obj = new ParentClass;
  80.  
  81. echo '<br />$obj->mymethod()<br />';
  82. echo $obj->mymethod();
  83.  
  84. echo '<br />$obj->mymethod(null,"test") <br />';
  85. echo $obj->mymethod(null,'test');
  86.  
  87. echo '<br /> $obj->mymethod("test","test","test")<br />';
  88. echo $obj->mymethod('test','test','test');
  89.  
  90. function($arg1, $lastname) {
  91. if(is_array($arg1)){
  92. $lastname = $arg1['lastname'];
  93. $firstname = $arg1['firstname'];
  94. } else {
  95. $firstname = $arg1;
  96. }
  97. ...
  98. }
  99.  
  100. <?php
  101.  
  102. /*******************************
  103. * author : hishamdalal@gmail.com
  104. * version : 3.7
  105. * date : 2014-12-01
  106. *****************************/
  107.  
  108. class Overloadable
  109. {
  110. static function call($obj, $method, $params=null) {
  111. // Get real method name
  112. $suffix_method_name = $method.self::getMethodSuffix($method, $params);
  113.  
  114. if (method_exists($obj, $suffix_method_name)) {
  115. // Call method
  116. return call_user_func_array(array($obj, $suffix_method_name), $params);
  117. #return $obj->$suffix_method_name($params);
  118. } else {
  119. $class = get_class($obj);
  120. throw new Exception('Tried to call unknown method '.$class.'::'.$suffix_method_name);
  121. }
  122. }
  123.  
  124. static function getMethodSuffix($method, $params_ary=array()) {
  125. $c = '__';
  126. if( is_array($params_ary) ) {
  127. foreach($params_ary as $i=>$param){
  128. // Adding special characters to the end of method name
  129. switch(gettype($param)){
  130. case 'array': $c .= 'a'; break;
  131. case 'boolean': $c .= 'b'; break;
  132. case 'double': $c .= 'd'; break;
  133. case 'integer': $c .= 'i'; break;
  134. case 'NULL': $c .= 'n'; break;
  135. case 'object': $c .= 'o'; break;
  136. case 'resource': $c .= 'r'; break;
  137. case 'string': $c .= 's'; break;
  138. }
  139. }
  140. }
  141. return $c;
  142. }
  143. // Get a reference variable by name
  144. static function &refAccess($var_name) {
  145. $r =& $GLOBALS["$var_name"];
  146. return $r;
  147. }
  148.  
  149. }
  150.  
  151.  
  152. class test
  153. {
  154. // Call Overloadable class
  155. // you must copy this method in your class to activate overloading
  156. function __call($method, $args) {
  157. return Overloadable::call($this, $method, $args);
  158. }
  159. // myFunction(void)
  160. function myFunction__() {
  161. echo 'myFunction(void)';
  162. }
  163. // myFunction(integer)
  164. function myFunction__i($int) {
  165. echo 'myFunction(integer='.$int.')';
  166. }
  167. // myFunction(string)
  168. function myFunction__s($string) {
  169. echo 'myFunction(string='.$string.')';
  170. }
  171. // myFunction(string)
  172. function myFunction__so($string, $object) {
  173. echo 'myFunction(string='.$string.', object='.get_class($object).')';
  174. echo '<pre>Object: ';
  175. print_r($object);
  176. echo '</pre>';
  177. }
  178. // anotherFunction_a(array)
  179. function anotherFunction__a($array) {
  180. echo 'anotherFunction('.print_r($array, 1).')';
  181. $array[0]++; // change the reference value
  182. $array['val']++; // change the reference value
  183. }
  184. // anotherFunction_si(string, integer)
  185. function anotherFunction__si($key, $value) {
  186. echo 'anotherFunction(string='.$key.', integer='.$value.')';
  187. // Get a reference
  188. $a2 =& Overloadable::refAccess($key); // $a2 =& $GLOBALS['val'];
  189. $a2 *= 3; // change the reference value
  190. }
  191. }
  192. //----------------------------------------------------------
  193. // Some data to work with:
  194. $val = 10;
  195. class obj {
  196. private $x=10;
  197. }
  198. //----------------------------------------------------------
  199.  
  200. // Start
  201. $t = new test;
  202.  
  203. // Call first method with no args:
  204. $t->myFunction();
  205. // Output: myFunction(void)
  206. echo '<hr>';
  207.  
  208. $t->myFunction($val);
  209. // Output: myFunction(integer=10)
  210. echo '<br>';
  211.  
  212. $t->myFunction("hello");
  213. // Output: myFunction(string=hello)
  214. echo '<br>';
  215.  
  216. $t->myFunction("str", new obj());
  217. /* Output:
  218. myFunction(string=str, object=obj)
  219. Object: obj Object
  220. (
  221. [x:obj:private] => 10
  222. )
  223. */
  224.  
  225. ## Passing by Reference:
  226.  
  227. echo '<hr>';
  228. echo '$val='.$val;
  229. // Output: $val=10
  230. echo '<br>';
  231. $t->anotherFunction(array(&$val, 'val'=>&$val));
  232. // Output: anotherFunction(Array ( [0] => 10 [val] => 10 ) )
  233. echo '<br>';
  234. echo '$val='.$val;
  235. // Output: $val=12
  236.  
  237. echo '<hr>';
  238. $t->anotherFunction('val', $val);
  239. // Output: anotherFunction(string=val, integer=12)
  240. echo '<br>';
  241. echo '$val='.$val;
  242. // Output: $val=36
  243.  
  244. /*******************************
  245. * author : hishamdalal@gmail.com
  246. * version : 3.5
  247. * date : 2014-11-16
  248. *****************************/
  249.  
  250. class Overloadable
  251. {
  252. // Magic Method
  253. public function __call($method, $params) {
  254. $class = get_class($this);
  255. // Get real method name
  256. $suffix_method_name = $method.$this->getMethodSuffix($method, $params);
  257.  
  258. if (method_exists($this, $suffix_method_name)){
  259. // Call method
  260. return call_user_func_array(array($this, $suffix_method_name), $params);
  261. }else{
  262. throw new Exception('Tried to call unknown method '.$class.'::'.$suffix_method_name);
  263. }
  264. }
  265.  
  266. function getMethodSuffix($method, $params_ary=array()){
  267. $c = '__';
  268. if(is_array($params_ary)){
  269. foreach($params_ary as $i=>$param){
  270. // Adding special characters to the end of method name
  271. switch(gettype($param)){
  272. case 'integer': $c .= 'i'; break;
  273. case 'double': $c .= 'd'; break;
  274. case 'string': $c .= 's'; break;
  275. case 'array': $c .= 'a'; break;
  276. case 'object': $c .= 'o'; break;
  277. case 'resource': $c .= 'r'; break;
  278. case 'NULL': $c .= 'n'; break;
  279. }
  280. }
  281. }
  282. return $c;
  283. }
  284. // Get reference variable by name
  285. function &refAccess($var_name){
  286. $r =& $GLOBALS["$var_name"];
  287. return $r;
  288. }
  289. }
  290.  
  291. //------------------------------------------------------------------------//
  292. ## Inherit "Overloadable" class to enable overloading
  293. class test extends Overloadable
  294. {
  295. // myFunction(void)
  296. function myFunction__() {
  297. echo "Hi<br>";
  298. }
  299.  
  300. // myFunction(integer, string)
  301. function myFunction__is($a, $s) {
  302. echo "$a, $s<br>";
  303. }
  304.  
  305. // myFunction(array)
  306. function myFunction__a($a) {
  307. $a[0]++; // change the reference value
  308. $a[1]++; // change value locally (method scope)
  309. }
  310.  
  311. // myFunction(integer)
  312. function myFunction__i($b) {
  313. // $GLOBALS['val2']
  314. $b2 =& $this->refAccess('val2');
  315. $b2 *= 3; // change the reference value
  316. }
  317.  
  318. // myFunction(string, string)
  319. function myFunction__ss($a, $b) {
  320. // $this->refAccess('obj')->plus()
  321. $this->refAccess($a)->plus();
  322.  
  323. #$b1 =& $GLOBALS["$b"];
  324. $b1 =& $this->refAccess($b);
  325. $b1++;
  326. $b1++;
  327. }
  328.  
  329. // myFunction(object, integer, array)
  330. function myFunction__oia($a, $b, $ary) {
  331. // Get arguments names
  332. $obj_name = $ary[0];
  333. $val_name = $ary[1];
  334.  
  335. // Get argument reference
  336. $a1 =& $this->refAccess($obj_name);
  337. $a1->plus();
  338.  
  339. $b1 =& $this->refAccess($val_name);
  340. $b1+= $b;
  341. }
  342.  
  343. // Just a normal method
  344. function welcome(){
  345. echo "Welcome!";
  346. }
  347.  
  348. }
  349. //------------------------------------------------------------------------//
  350. // Some data types to work with:
  351. class obj {
  352. private $v=0;
  353. function plus(){
  354. $this->v++;
  355. }
  356. function result(){
  357. return $this->v;
  358. }
  359. }
  360. $val = 10;
  361. $val2 = 10;
  362. $obj = new obj();
  363.  
  364. // Show Default values
  365. echo "$val = $val, $val2 = $val2, ";
  366. echo 'obj->v =' . $obj->result()."<hr>";
  367. //------------------------------------------------------------------------//
  368.  
  369. // Start
  370. $t = new test();
  371.  
  372.  
  373. // Call first method with no args:
  374. echo 'myFunction__():<br>';
  375. $t->myFunction();
  376. echo "<hr>";
  377.  
  378.  
  379. echo 'myFunction__is(integer, string):<br>';
  380. $t->myFunction($val, 'text');
  381. echo "$val = $val, $val2 = $val2<hr>";
  382.  
  383.  
  384. echo 'myFunction__i(integer):<br>';
  385. $t->myFunction($val);
  386. echo "$val = $val, $val2 = $val2<hr>";
  387.  
  388. ## Passing by Reference:
  389.  
  390. // 1) The best way to pass arguments
  391. echo 'myFunction__a(array):<br>';
  392. //Passing first item by reference
  393. $t->myFunction(array(&$val, $val2));
  394. echo "$val = $val, $val2 = $val2<hr>";
  395.  
  396. // 2) Passing arguments names
  397. echo 'myFunction__ss(string, string):<br>';
  398. // Pass object name and variable name
  399. $t->myFunction('obj', 'val');
  400. echo "$val = $val, $val2 = $val2, ";
  401. echo 'obj->v =' . $obj->result()."<hr>";
  402.  
  403. // 3) Passing arguments values and names
  404. echo 'myFunction__oia(object, integer, array):<br>';
  405. // Pass object, integer values and passing there names as array
  406. $t->myFunction($obj, $val, array('obj', 'val'));
  407. echo "$val = $val, $val2 = $val2, ";
  408. echo 'obj->v =' . $obj->result()."<hr>";
  409.  
  410. // Just a normal method
  411. echo 'welcome():<br>';
  412. $t->welcome();
  413.  
  414. /*******************************
  415. * author : hishamdalal@gmail.com
  416. * version : 3.1
  417. * date : 2013-04-11
  418. *****************************/
  419.  
  420. class overloadable
  421. {
  422. protected $fname = null;
  423. protected $fargs = array();
  424.  
  425. //--------------------------------------------------//
  426. function set($obj, $fname, $args){
  427. $n = '';
  428. $type = $this->getType($args);
  429. $n = "$o = new $obj();n";
  430. $n .= "if(method_exists($o, '$fname"."_$type')){n";
  431. $n .= "t$r = $o->$fname"."_$type(". $this->getArgsName($args) .");n";
  432. $n .= "}else{nt$r = null;n";
  433. $n .= "ttrigger_error('function ".$fname."_".$type." is not exist!');n}";
  434. eval("$r = $n;");
  435. return $r;
  436. }
  437. //--------------------------------------------------//
  438. function getType($args) {
  439. $argType = array();
  440. foreach($args as $i=>$val) {
  441. $argType[$i][] = $this->getSuffix($val, $i) ;
  442. }
  443. $s = '';
  444. if(is_array($argType)){
  445. foreach($argType as $type){
  446. $s .= implode('', $type);
  447. }
  448. return $s;
  449. }
  450. return implode('', $argType);
  451. }
  452. //--------------------------------------------------//
  453. function getSuffix($byValarg, $i) {
  454. if( is_numeric($byValarg) ) {
  455. $type = 'N';
  456. $this->fargs['N'.$i] = $byValarg;
  457. } elseif( is_array($byValarg) ) {
  458. $type = 'A';
  459. $this->fargs['A'.$i] = $byValarg;
  460. } elseif( is_object($byValarg) ) {
  461. $type = 'O';
  462. $this->fargs['O'.$i] = $byValarg;
  463. } elseif( is_resource($byValarg) ) {
  464. $type = 'R';
  465. $this->fargs['R'.$i] = $byValarg;
  466. } else {
  467. $type = 'S';
  468. $this->fargs['S'.$i] = $byValarg;
  469. }
  470. return $type;
  471. }
  472. //--------------------------------------------------//
  473. function getArgsName($args){
  474. $r = array();
  475. $ary = array_keys($this->fargs);
  476. foreach( $ary as $k=>$v){
  477. $r[]='$this->fargs["'.$v.'"]';
  478. }
  479. return implode(", ", $r);
  480. }
  481. //--------------------------------------------------//
  482. function __call($name, $args){
  483. $this->fargs = array();
  484. return $this->set(get_class($this), $name, $args);
  485. }
  486. //--------------------------------------------------//
  487. }
  488.  
  489.  
  490. class test2 extends overloadable {
  491. function foo_(){
  492. echo 'foo - no args';
  493. }
  494. function foo_S($s){
  495. echo "foo - one string $s";
  496. }
  497. function foo_SS($s1, $s2){
  498. echo "foo - tow strings $s1, $s2";
  499. }
  500. function foo_SN($s, $n){
  501. echo "foo - string and number $s, $n";
  502. }
  503. function foo_A($ary){
  504. print_r($ary);
  505. }
  506. function foo_AA($ary1, $ary2){
  507. if(is_array($ary1) && is_array($ary2)){
  508. echo "foo - tow arrays";
  509. }else{echo 0;}
  510. }
  511. function foo_O($obj){
  512. echo "foo - ";
  513. print_r($obj);
  514. }
  515. function hi(){
  516. echo "hi - welcome!";
  517. }
  518. }
  519.  
  520. echo '<pre>';
  521. $t = new test2();
  522.  
  523. echo '<br />foo_: ';
  524. print_r( $t->foo() );
  525.  
  526. echo '<br />foo_s: ';
  527. print_r( $t->foo('a') );
  528.  
  529. echo '<br />foo_ss: ';
  530. print_r( $t->foo('a', 'b') );
  531.  
  532. echo '<br />foo_sn: ';
  533. print_r( $t->foo('a', 2) );
  534.  
  535. echo '<br />foo_snn: ';
  536. print_r( $t->foo('s', 2, 9) );
  537.  
  538. echo '<br />foo_a: ';
  539. print_r( $t->foo(array(4,5,6,7)) );
  540.  
  541. echo '<br />foo_aa: ';
  542. print_r( $t->foo( array(5,6,7), array(8,9,10) ) );
  543.  
  544. echo '<br />foo_o: ';
  545. print_r( $t->foo($t) );
  546.  
  547. echo '<br />hi: ';
  548. print_r( $t->hi() );
  549.  
  550. function($arg = NULL) {
  551.  
  552. if ($arg != NULL) {
  553. etc.
  554. etc.
  555. }
  556. }
  557.  
  558. <?php
  559.  
  560. class abs
  561. {
  562. public function volume($arg1=null, $arg2=null, $arg3=null)
  563. {
  564. if($arg1 == null && $arg2 == null && $arg3 == null)
  565. {
  566. echo "function has no arguments. <br>";
  567. }
  568.  
  569. else if($arg1 != null && $arg2 != null && $arg3 != null)
  570. {
  571. $volume=$arg1*$arg2*$arg3;
  572. echo "volume of a cuboid ".$volume ."<br>";
  573. }
  574. else if($arg1 != null && $arg2 != null)
  575. {
  576. $area=$arg1*$arg2;
  577. echo "area of square = " .$area ."<br>";
  578. }
  579. else if($arg1 != null)
  580. {
  581. $volume=$arg1*$arg1*$arg1;
  582. echo "volume of a cube = ".$volume ."<br>";
  583. }
  584.  
  585.  
  586. }
  587.  
  588.  
  589. }
  590.  
  591. $obj=new abs();
  592. echo "For no arguments. <br>";
  593. $obj->volume();
  594. echo "For one arguments. <br>";
  595. $obj->volume(3);
  596. echo "For two arguments. <br>";
  597. $obj->volume(3,4);
  598. echo "For three arguments. <br>";
  599. $obj->volume(3,4,5);
  600. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement