Jump to content

Why does this happen?


ryy705

Recommended Posts

Hello,

 

  I expected the following code to display a:40 b:10  but it displays  a: 30, b: 10.  It seems that only  $this->a is getting passed to  parent::changeValue() instead of  the sum of $this->a + $this->b.  Whys is that?

 

<?php

class ClassOne {
  protected $a = 10;

  public function changeValue($b) {
    $this->a = $b;
  }
}

class ClassTwo extends ClassOne {

  protected $b = 10;

  public function changeValue($b) {
    $this->b = 10;
    parent::changeValue($this->a + $this->b);
  }

  public function displayValues() {
    print "a: {$this->a}, b: {$this->b}\n";
  }
}

$obj = new ClassTwo();

$obj->changeValue(20);
$obj->changeValue(10);

$obj->displayValues();

Link to comment
https://forums.phpfreaks.com/topic/133449-why-does-this-happen/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.