National_Instruments
200-500 · Question #32
What is the error in the following declaration of a static class method? 1 <?php 2 class car { 3 static $speeds = array( 4 'fast', 5 'slow', 6 'medium', 7 ); 9 static function getSpeeds() 10 { 11…
The correct answer is B. The use of $this from within static methods is not possible. See the full explanation below for the reasoning.
Question
What is the error in the following declaration of a static class method? 1 <?php 2 class car { 3 static $speeds = array( 4 'fast', 5 'slow', 6 'medium', 7 ); 9 static function getSpeeds() 10 { 11 return $this->speeds; 12 } 13 } 14 ?>
Options
- AStatic methods must not return a value.
- BThe use of $this from within static methods is not possible.
- CStatic methods need the method keyword instead of the function keyword.
- DThere is no static keyword in PHP.
How the community answered
(32 responses)- A3% (1)
- B81% (26)
- C13% (4)
- D3% (1)
Community Discussion
No community discussion yet for this question.