Is it possible to return a specific type class from function?
class barCl {
function _construct() {
}
}
function fooBar() {
return (barCl) new barCl;
//^^^^^^^
}
Return types have just been accepted into PHP 7. The return type is part of the function declaration, not its definition. Unless you're making an abstract function or interface, that won't make much difference.
Combining your code with what's in that RFC, you would do:
class barCl {
function _construct() {
}
}
function fooBar(): barC1 {
return new barCl;
}
In php versions before 7, it is not possible to strictly declare a return type for a callable.
As you already do it you can just cast it like this:
(To explicit set the type of a variable in php is not possible! Also you can't cast your variable to your Object type! The nearest cast would be (object)
which you can do)
return (object) $xyObj;
You can read more about PHP type juggling: http://php.net/manual/en/language.types.type-juggling.php
And a quote from there:
a variable's type is determined by the context in which the variable is used
FYI:
You missed one underscore in your constructor:
function _construct() {
//^ Here you need 2x _ as all magic methods do
}