How to don't pass any value to methods in PHP -
i have simple class 1 method accepts parameter.
class articlecontroller { public function show(article $article) { // logic here } } and want use in way:
$articlecontroller = new articlecontroller; $articlecontroller->show(); as see didnt pass value method "show". since php7 out , forces pass value(dependency), there way dont pass value method , still can use without error ? php version = 7.1.8
the behaviour described in question not related php 7 in way. works same on php 5 well.
you can set default value argument $article (making optional way) if set null have make sure handle value in method:
class articlecontroller { public function show(article $article = null) { if (isset($article)) { // logic here, $article object } else { // $article null, cannot use object } } } when call show() without passing value, argument $article initialized default value (null in example).
$controller = new articlecontroller(); $news = new article(); $controller->show($news); // inside show() method, $article set $news $controller->show(); // no argument passed, $article null inside show()
Comments
Post a Comment