Hello All,
I have a general OOP question.
If I have the following classes in C#
class Parent
{
public string val = "Parent";
public void getValue()
{
Console.WriteLine(this.val);
}
}
class Child:Parent
{
public string val = "Child";
}
Child child = new Child();
child.getValue();
The code outputs 'Parent'. As I understand it's because this points to Parent object, right?
If I do the same in PHP5:
class ParentClass {
public $val = 'parent';
public function foo()
{
echo $this->val;
}
}
class ChildClass extends ParentClass {
public $val = 'child';
}
$a = new ChildClass();
$a->foo();
The result will be 'child'.
Though if I change
public $val = 'parent';
to
private $val = 'parent';
then PHP will also show 'parent'. C# always return 'parent' with both public and private access modifiers.
Is there any reason for this? And which behavior is correct?
Any useful links to read will be highly appreciated!
Thank you!