CyberShot Posted April 16, 2020 Share Posted April 16, 2020 is there a general rule as to when to set a variable? I was watching a tutorial today and the author made a small function that would accept a string. Why would you set the string in the function like that? Would it work if it was just $string? function e($string=""){ do something return $string; } Quote Link to comment Share on other sites More sharing options...
kicken Posted April 16, 2020 Share Posted April 16, 2020 Defining a value in the parameter list makes that parameter optional. If it's not provided when the function is called, the it takes on the value assigned to it. Your specific example doesn't really make use of the feature effectively. Take something like this for example though: function findFiles($directory, $includeHidden = false){ $iter = new DirectoryIterator($directory); $list = []; foreach ($iter as $item){ if ($item->isFile()){ $isHidden = $item->getFilename()[0] === '.'; if ($includeHidden || !$isHidden){ $list[] = $item->getPathname(); } } } return $list; } That function requires at least one parameter when it's called, the directory to search. So you end up with the following options for calling it $files = findFiles('/home/kicken'); /* executes with $directory = '/home/kicken', $includeHidden = false */ $files = findFiles('/home/aoeex', true); /* executes with $directory = '/home/aoeex', $includeHidden = true */ 2 Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 16, 2020 Share Posted April 16, 2020 Excellent question, with great answer from Kicken. As a rule of thumb this is a way to allow for optional parameters, so to directly answer your question, typically you just have the parameter and not a default value. More importantly PHP now has support for scalar type hinting, which is an important upgrade in 7.1. It had class type hinting since php 5. //PHP5 class typehint class Student { private firstName; private lastName; public function __construct($firstName, $lastName) { $this->firstName = $firstName; $this->lastName = $lastName; } public function getFirstName() { return $this->firstName; } public function getLastName() { return $this->lastName; } } class SchoolClass { private $name; private $year; private $students = array(); public function __construct($name, $year) { $this->name = $name; $this->year = $year; } public function enrollStudent(Student $student) { $this->students[] = $student; } public function listStudents() { $output = ''; foreach($this->students as $student) { $output .= $student->getLastName() . ', ' . $student->getFirstName() . PHP_EOL; } return $output; } } // Example $historyClass = new SchoolClass('AP History', 2020); $student = new Student('Mike', 'Smith'); $historyClass->enrollStudent($student); // This will throw a Catchable fatal error $historyClass->enrollStudent(array('Bob', 'Jones')); With PHP 7, you actually have scalar type hinting. So for example <?php declare(strict_types=1); // has to be 1st line of file class SchoolClass { private $name; private $year; private $students = array(); public function __construct(string $name, integer $year) { $this->name = $name; $this->year = $year; } } // Ok $geometry = new SchoolClass('Geometry 1', 2020); // This produces an error because $year must be an integer value and not a string $bio = new SchoolClass('Biology 1', '2020-02-01'); The declare(strict_types=1) call turns on strict mode, so that type coercion won't happen on parameters. Without it, passing parameters like this won't cause an error. It's up to you as to whether you want the types to be strictly checked or not. Without strict_mode, this would work. $bio = new SchoolClass('Biology 1', '2020'); This is a nice feature as well: class SchoolClass { /* assume previous definition */ public function listStudents(): string { $output = ''; foreach($this->students as $student) { $output .= $student->getLastName() . ', ' . $student->getFirstName() . PHP_EOL; } return $output; } } This hints that the return value of listStudents() has to be string, which can help with autosense in editors, and will throw a runtime error if something malfunctions such that $obj->listStudents() either doesn't return a string as it is suppossed to, or if the function is used as a input to a function that expects a parameter that is NOT a string. These seem like simple examples in a small project, but as a project grows in size and complexity, these small improvements can help you catch logic errors sooner than later and also make smart editors better at their job of helping you code using an extensive class library. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.