When i catch class in an method i am getting error. When i call anotherMethod i got error like this.
See the bellow code. How can i solve this please help.
class Java{
function anotherMethod(Php $phpAccess){
$phpAccess->framework();
$phpAccess->cms();
}
}
class Php{
public function framework()
{
echo "Laravel is a popular php framework. </br>";
}
public function cms()
{
echo "WordPress is popular php cms. </br>";
}
}
$php = new Php();
$java = new Java($php);
echo $java->anotherMethod();
But when i catch class in an constructor then it's giving right output.See this code bellow.
class Java{
function __construct(Php $phpAccess){
$phpAccess->framework();
$phpAccess->cms();
}
}
You are getting errors because you are passing the PHP object to the Java constructor but the PHP object is a dependency for the anotherMethod instead. Change the Java class as below to work as you want:
class Java
{
private $phpAccess;
public function __construct(Php $phpAccess)
{
$this->phpAccess = $phpAccess;
}
public function anotherMethod()
{
$this->phpAccess->framework();
$this->phpAccess->cms();
}
}
class Php
{
public function framework()
{
echo "Laravel is a popular php framework. </br>";
}
public function cms()
{
echo "WordPress is popular php cms. </br>";
}
}
$php = new Php();
$java = new Java($php);
$java->anotherMethod();
You call function anotherMethod without any parameter. In your class the method is defined WITH paramater. So you have to call anotherMethod WITH parameter $php
echo $java->anotherMethod($php);