Basic PHP OOPS Query
Ok. I am starting out OOPS in PHP. Created a couple of classes: customer(parent) and sales(child) class that inherits from parent cla开发者_Python百科ss. 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
Beyond the fact extending a Customer class to a Sale is a non-sense, it shows nothing because the name member is private
private means not available outside the current class neither to their children, if you want to protect your member but let them available to your extended class you should use the protected
keyword.
By the way, when using variable in string you should rather concatenate:
echo 'Hello ' . $this->name . ' this is a print of your purchased products';
or use variable substitution:
echo "Hello {$this->name} this is a print of your purchased products";
It is more readable.
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();
Better you can read http://Confuseoops.blogspot.com . Lot of example there
when ever people try to learn OOPS ? They will get confuse them self by keyword and design patten
$name is private, that's why you can't have access to it from child class sales. Change it to protected will make $name visible to children classes, or make it public to make it visible to any classes.
2 problems. Your superclass has private variables, therefore only that class can access those variables. Use protected or public. Also this string is incorrect as far as the $this property accessor works.
echo "Hello ".$this->name. " this is a print of your purchased products";
Ok thanks all..changing from private to protected solved it for me....as some complained that the program design is incorrect but this was just to test my understanding of how inheritance in PHP works and how PHP would include parent class's code into child class code during runtime. There was no intention to give any consideration to design of the application...but still that's a thing to keep in mind for future...BACK TO THE FUTURE>>>
精彩评论