Complete the code to define a constant named VERSION inside the interface.
<?php
interface AppInfo {
const [1] = '1.0';
}
?>The constant name inside an interface should be uppercase and descriptive. 'VERSION' is the correct constant name here.
Complete the code to access the constant MAX_USERS from the interface UserLimits.
<?php
interface UserLimits {
const MAX_USERS = 100;
}
echo UserLimits::[1];
?>Constants are accessed with the interface name followed by :: and the constant name exactly as defined, which is 'MAX_USERS'.
Fix the error in the code by completing the blank to correctly access the constant.
<?php
interface Config {
const TIMEOUT = 30;
}
class App implements Config {
public function getTimeout() {
return self::[1];
}
}
?>Inside the class implementing the interface, constants from the interface are accessed with self::CONSTANT_NAME. So 'TIMEOUT' is correct.
Fill both blanks to create a dictionary of constants from the interface with a condition.
<?php
interface StatusCodes {
const OK = 200;
const NOT_FOUND = 404;
const ERROR = 500;
}
$codes = [
'success' => StatusCodes::[1],
'failure' => StatusCodes::[2]
];
?>'success' should map to the OK code (200), and 'failure' should map to ERROR (500) from the interface constants.
Fill all three blanks to define an interface with constants and use them in a class method.
<?php
interface Settings {
const [1] = 'dark';
const [2] = 24;
}
class UserSettings implements Settings {
public function getTheme() {
return self::[3];
}
}
?>The interface defines THEME and FONT_SIZE constants. The class method returns THEME using self::THEME.