Is there anything to stop me from type hinting like this within the __construct() parentheses?
<?php
class SomeClass extends BaseClass {
public function __construct(array $someArray) {
parent::__construct($someArray);
}
Or can I only do it like this?
<?php
class SomeClass extends BaseClass {
public function __construct($someArray = array()) {
parent::__construct($someArray);
}
Edit:
This is what works: (Thanks @hakra and @Leigh)
<?php
class SomeClass extends BaseClass {
public function __construct( array $someArray = NULL ) {
parent::__construct( (array) $someArray);
}
To me it looks nice and clean and I know exactly what it supposed to mean.
You can do so:
The case here is to make use of the
= NULLexception of the rule. The next line:is just some shorthand to convert
NULLinto an empty array and otherwise leave the array an array as-is (a so called castDocs to array):(from: Converting to arrayDocs)
Sure you could write it even shorter:
but it does not explain it that well.