Basic PHP OOPS Query

Posted by appu on Stack Overflow See other posts from Stack Overflow or by appu
Published on 2011-03-12T16:09:32Z Indexed on 2011/03/12 16:10 UTC
Read the original article Hit count: 252

Filed under:
|

Ok. I am starting out OOPS in PHP. Created a couple of classes: customer(parent) and sales(child) class that inherits from parent class. Created another testcustomer.php in which a new sales object is created however the salesprint() function defined in the sales class does not echo out customer's name though it is set to be "Jane" in the class.customer.php(parent). My thinking is that when sales class extends customer class PHP automatically includes all the code from class.customer.php to sales.customer.php and therefore the constructor in parent class set $name to "Jane".

Here is the code: class.customer.php

   <?php

     class customer{

       private $name;
       private $cust_no;

     public function __construct($customerid) {

        $this->name = 'Jane';
        $this->cust_no = $customerid;

       }

    }
  ?>

class.sales.php

    <?php

        require_once('class.customer.php');
        class sales extends customer{

        public function salesprint($customerid) {
    echo "Hello $this->name this is a print of your purchased products";

        }

     }

   ?>

testcustomer.php

      require_once('class.sales.php');
      $objsales = new sales(17);
      $objsales->salesprint(17);

      ?>

The Output I get Hello this is a print of your purchased products.

What am i doing wrong ? thanks romesh

© Stack Overflow or respective owner

Related posts about php

Related posts about oops