In PHP, how can we define a field as a certian class's instance like JAVA?
Get the solution ↓↓↓Solution:
If your goal is auto-completion you can go with DocBlocks.
class AppleTree
{
/**
* @var Apple
*/
public $apple;
}
class Apple
{
/**
* @return Color
*/
public function getColor() {}
}
However if you want to achieve the behavior that restricts setting anything but instances of given class you have to use type hinting e.g.
class AppleTree
{
/**
* @var Apple
*/
protected $apple;
public function setApple(Apple $apple)
{
$this->apple = $apple;
}
/**
* @return Apple
*/
public function getApple()
{
return $this->apple;
}
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: dompdf image not found or type unknown
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.