Co prawda rozwiązanie mojego problemu znalazłem ale tylko w PHP 7. Chciałbym jednak żeby kod działał także na wcześniejszych wersjach.
W php7 wykonałem to tak:
<?php class test_class { function arg(int $var, string $var2, string $var3) { if($var && $var2 && $var3) { return 'OK, function execute correct'; } else { return 'ERROR, if variable is empty or not exist I not want execute method!'; } } } $object = new test_class(); $var = 50; $var2 = 50; //$var3 = 50; http://www.php.net/echo $object->arg($var,$var2,$var3); ?>
Fatal error: Uncaught TypeError: Argument 3 passed to test_class::arg() must be of the type string, null given, called in D:\xampp\htdocs\argument.php on line 26 and http://www.php.net/defined in D:\xampp\htdocs\argument.php:5 Stack trace: #0 D:\xampp\htdocs\argument.php(26): test_class->arg(50, '50', NULL) #1 {main} thrown in D:\xampp\htdocs\argument.php on line 5
<?php class test_class { function arg($var, $var2, $var3) { // My temporary solution $list = http://www.php.net/func_get_args(); $count = 0; foreach ($list as $value) { if($value) $count++; } if($count != http://www.php.net/func_num_args()){ return false; } // My temporary solution end if($var && $var2 && $var3) { return 'OK, function execute correct'; } else { return 'ERROR, if variable is empty or not exist I not want execute method!'; } } } $object = new test_class(); $var = 50; $var2 = 50; //$var3 = 50; http://www.php.net/echo $object->arg($var,$var2,$var3); ?>
Ustaw domyślną wartość na null
<?php function arg($arg = null, $arg2 = null) { if (!http://www.php.net/isset($arg, $arg2)) { return 'error'; } } http://www.php.net/echo arg(0, 0)."\n"; http://www.php.net/echo arg(0)."\n"; http://www.php.net/echo arg();
E...
<?php class test_class { function arg($var, $var2, $var3) { if(!http://www.php.net/isset($var, $var2, $var3)) { return 'NOT'; } return 'OK'; } } $object = new test_class(); $var = 50; $var2 = 50; //$var3 = 50; http://www.php.net/echo $object->arg($var,$var2,$var3);
<?php class test_class { function arg($var, $var2, $var3) { if(!http://www.php.net/isset($var, $var2, $var3)) { return 'NOT'; } return 'OK'; } } $object = new test_class(); $var = 50; $var2 = 50; //$var3 = 50; http://www.php.net/echo $object->arg($var,$var2,$var3);
<?php class test_class { function check($list) { // My temporary solution foreach ($list as $value) { if (!$value) return true; } // My temporary solution end } function arg($var, $var2, $var3) { if ( $this->check( http://www.php.net/func_get_args() ) ) return false; if ($var && $var2 && $var3) { return 'OK, function execute correct'; } else { return 'ERROR, if variable is empty or not exist I not want execute method!'; } } } $object = new test_class(); $var = 50; $var2 = 50; //$var3 = 50; http://www.php.net/echo $object->arg($var, $var2, $var3); ?>
http://php.net/manual/en/reflectionfunctionabstract.getnumberofparameters.php
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)