Coding Test
Nothing to see here, just testing some code highlighting. 😄
<?php
/**
* This is a docblock comment.
*/
// Single-line comment
# Another single-line comment
class MyClass extends AnotherClass implements InterfaceOne, InterfaceTwo {
const MY_CONSTANT = 123;
public $myPublicProperty = "hello";
protected $_myProtectedProperty = 45.67;
private static $_myPrivateStaticProperty = true;
/**
* Constructor method.
* @param string $param1 Description of parameter 1.
* @param int $param2 Description of parameter 2.
*/
public function __construct(string $param1, int $param2) {
$this->myPublicProperty = $param1;
$this->_myProtectedProperty = $param2;
}
/**
* A public method.
* @return array An array of values.
*/
public function myPublicMethod(): array {
$localVariable = null;
$anotherLocalVariable = 0;
if ($localVariable === null && $anotherLocalVariable !== 10) {
echo "Condition is true!\n";
} elseif ($anotherLocalVariable > 5) {
print 'Another condition is true!';
} else {
/*
* Multi-line comment within a method.
*/
trigger_error("Something went wrong.", E_USER_WARNING);
}
switch ($anotherLocalVariable) {
case 0:
break;
case 1:
continue;
default:
return [$this->myPublicProperty, $this->_myProtectedProperty];
}
for ($i = 0; $i < 5; $i++) {
// Loop
}
while ($anotherLocalVariable < 20) {
$anotherLocalVariable++;
}
do {
// Do-while loop
} while (false);
foreach (['a', 'b', 'c'] as $value) {
// Foreach loop
}
try {
throw new Exception("An error occurred.");
} catch (Exception $e) {
echo "Caught exception: " . $e->getMessage() . "\n";
} finally {
// Finally block
}
$heredocString = <<<EOD
This is a heredoc string.
It can span multiple lines.
Variables like {$this->myPublicProperty} are interpolated.
EOD;
$nowdocString = <<<'EOD'
This is a nowdoc string.
Variables like {$this->myPublicProperty} are NOT interpolated.
EOD;
$shortArray = [1, 2, 3];
$longArray = array("key" => "value", 4 => "another");
return [$heredocString, $nowdocString, $shortArray, $longArray];
}
private function _myPrivateMethod() {
// Private method content
}
}
function my_global_function(string $arg1, int $arg2 = 0): bool {
return $arg1 === (string) $arg2;
}
$myObject = new MyClass("test", 99);
$result = $myObject->myPublicMethod();
print_r($result);
unset($myObject);
define('GLOBAL_CONSTANT', 'some value');
// Operators
$a = 1 + 2 - 3 * 4 / 5 % 6;
$b = $a++;
$c = --$b;
$d = $a == $b && $c != $d || $a > $b;
$e = $d ? 'yes' : 'no';
// Type hinting and casting
$f = (int) "123";
$g = (string) 456;
$h = (array) new stdClass();
?>