class LinkedList{
private $first;
public function Merge(LinkedList $ll){
//We can't access $ll->first for merging operation as it's private inside $ll.
//We can only access $this->first not $first of $ll
}
}
What could be the solution for this?
- Make $first public? that is not a nice idea
- Implement getter method for $first property
Can someone please suggest me correct way to access $first?
Access is controlled though the class, not the instance. You can access any private member of a class from code that’s within the class. Whether you use the
$thisreference or another doesn’t matter.This is the same in PHP, Java, and C++.