Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4049286
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:55:33+00:00 2026-05-20T13:55:33+00:00

Ok. I am starting out OOPS in PHP. Created a couple of classes: customer(parent)

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T13:55:34+00:00Added an answer on May 20, 2026 at 1:55 pm

    Because the $name field is declared as private, it is not inherited to the subclass. Change it to protected.

    Also, I would suggest that you do the same for the $cust_no field. By doing this, you will avoid passing $customerid as an argument to the salesprint method and therefore your final code will look like this:

    $objsales = new sales(17);
    $objsales->salesprint();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just starting out in asp.net. Have just created a login.aspx page in my site
So I am just starting out developing PHP web applications and have finished setting
Just starting out with C#, so this may be overly simple that I keep
I am starting out with PRADO php framework and i have a quick question:
i am starting out with opengles coming from old opengl. i see there is
I'm just starting out learning ASP.NET. From what I understand, ASP.NET differs from old
When starting out with a new application, would you rather just use an existing
I am just starting out with DI & unit testing and have hit a
I am just starting out with Silverlight (2 RC0) and can’t seem to get
I'm just starting out playing around with Linq Expressions and I've hit a wall.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.