I currently have two php class files and I’m working on accessing functions across them. Currently, in the secondClass.php file, I’m able to call a function in firstClass.php successfully and that class then calls another in the same firstClass.php file which is expected to return a result
I can access the function successfully, but when I check if the firstClass function isLoaded
is true or access it I get an error because it’s a private function
How can I get the returned object from this private function in firstClass.php?
firstClass.php
private function loadData($id){
return $this->isLoaded();
}
private function isLoaded(){
$var = 123;
return $var;
}
secondClass.php
public function getResult($id){
$firstC = new firstClass();
$result = 'ABC';
$newResult = $firstC->loadData($result);
if($firstC->isloaded){
echo('we are good');
}
}
Advertisement
Answer
You could add a property to the first class that is public.
public isLoaded = false;
Then you could change that value using your method. And I think technically your method name there is flirting with being a verb vs being a noun. You could update the existing isLoaded method to be something like checkIsLoaded which sets the public property to true or false.
public isLoaded = false;
private function loadData($id){
return $this->isLoaded();
}
private function checkIsLoaded(){
$var = 123;
$this->isLoaded = $var;
}
Now you can access it with the same format you have:
$firstC->isloaded;