RSS

Another PHP5 and C# OOP difference

28 Jun

A few days ago I have found out for myself about one more difference between PHP and C# OOP impelentations. Look at the following code snippet in PHP:

class ParentClass {
    public $val = 'Parent';
	
    public function GetValue()
    {
        echo $this->val;
    }
}

class ChildClass extends ParentClass {
     public $val = 'Child';
}

$a = new ChildClass();
$a->GetValue();

What do you expect it to show – ‘Parent’ or ‘Child’? The PHP manual says the result will be ‘Child’. And it seems quite logical to me as I would expect public property to be overridden.

Now rewrite the same code to 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();

Run the code and you will get ‘Parent’…

Change access modifier of val to private in the parent class. Now both PHP and C# outputs the same – ‘Parent’. Why?

First thing to mention is that unlike PHP, public identifiers cannot be overridden in C#. Calling GetValue(), this points to parent object and returns “not overridden” value of val as a result. Private identifers inside parent are not accessible for the child and therefore are not overridden so everything becomes clear.

To make C# behavior similar to PHP you should either add a method with the same signature and with override keyword to Child class and mark parent method as virtual:

class Parent   {
     public string val = "Parent";

     public virtual void GetValue()  {
            Console.WriteLine(this.val);
     }
}

class Child:Parent  {
     public string val = "Child";

     public override void GetValue() {
         Console.WriteLine(this.val);
     }
}

or use overridden public properties to access the val value. This will work correctly as unlike public fields, public properties can be overridden.

class Parent {
    private string privateVal = "Parent";
    public virtual string val
    {
        get { return privateVal; }
    }

    public void GetValue()
    {
        Console.WriteLine(this.val);
    }
}

class Child : Parent {
    private string privateVal = "Child";
    public override string val
    {
        get { return privateVal; }
    }
}

I couln’t find any “standard” behaviour for this case. So I guess it was up to C# and PHP teams how to implement this.

Advertisements
 
2 Comments

Posted by on June 28, 2009 in ASP.NET, PHP

 

Tags: , ,

2 responses to “Another PHP5 and C# OOP difference

  1. u

    July 3, 2009 at 12:19 pm

    Your first C# example: GetValue in a Parent Child has no knowledge about children’s variables. And it must not know that.

    The only way to have polymorphic behavior (dynamic resolution) is through usage of virtual functions. All other ways are statically resolved. This _is_ standard behavior. So when you don’t define virtual getters your base class will always use static binding to member variables (or member get functions).

     
  2. martani_net

    August 1, 2009 at 10:41 pm

    I actually loved ur blog! though I think php can never be a read POO language :p

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: