0
0
PHPprogramming~10 mins

Interface constants in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a constant named VERSION inside the interface.

PHP
<?php
interface AppInfo {
    const [1] = '1.0';
}
?>
Drag options to blanks, or click blank then click option'
ACONST_VERSION
BVERSION
CAppVersion
Dversion
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase names for constants.
Trying to use variables instead of constants.
2fill in blank
medium

Complete the code to access the constant MAX_USERS from the interface UserLimits.

PHP
<?php
interface UserLimits {
    const MAX_USERS = 100;
}

echo UserLimits::[1];
?>
Drag options to blanks, or click blank then click option'
AMAX_USERS
BMaxUsers
Cmax_users
DmaxUsers
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or camelCase names when accessing constants.
Trying to access constants as variables.
3fill in blank
hard

Fix the error in the code by completing the blank to correctly access the constant.

PHP
<?php
interface Config {
    const TIMEOUT = 30;
}

class App implements Config {
    public function getTimeout() {
        return self::[1];
    }
}
?>
Drag options to blanks, or click blank then click option'
Atimeout
BConfig::TIMEOUT
CTIMEOUT
Dself::TIMEOUT
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to use the interface name with :: inside the class method.
Using lowercase constant names.
4fill in blank
hard

Fill both blanks to create a dictionary of constants from the interface with a condition.

PHP
<?php
interface StatusCodes {
    const OK = 200;
    const NOT_FOUND = 404;
    const ERROR = 500;
}

$codes = [
    'success' => StatusCodes::[1],
    'failure' => StatusCodes::[2]
];
?>
Drag options to blanks, or click blank then click option'
AOK
BNOT_FOUND
CERROR
DSUCCESS
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined constant names.
Mixing up the success and failure codes.
5fill in blank
hard

Fill all three blanks to define an interface with constants and use them in a class method.

PHP
<?php
interface Settings {
    const [1] = 'dark';
    const [2] = 24;
}

class UserSettings implements Settings {
    public function getTheme() {
        return self::[3];
    }
}
?>
Drag options to blanks, or click blank then click option'
ATHEME
BFONT_SIZE
DFONT
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent constant names between interface and class.
Using lowercase or partial names.