Ive been reading through "Learn PHP 4 in 24 hours" and working through the examples and end of chapter review and have come across a question that has me stumped.
The chapter deals with Objects. The activity goes like this: Create a class that stores 2 numbers, display the numbers. The create another class that inherits some properties and adds a new one, addition and print the result of adding the 2 numbers to the screen.
I should mention that i am not in a school program this is self study.
The problem is im not sure how i am supposed to do this, i dont have the answer book.
Here is my code. This prints the two numbers and then does the addition. all the examples in the chapter just use hard coded numbers.
Does this make sense? So far i have read about IF, FOREACH, ARRAYS, FUNCTIONS, and VARIABLES. It just seems kind of stupid to write all this code to do something that could be done in a few lines.
-Dave
The chapter deals with Objects. The activity goes like this: Create a class that stores 2 numbers, display the numbers. The create another class that inherits some properties and adds a new one, addition and print the result of adding the 2 numbers to the screen.
I should mention that i am not in a school program this is self study.
The problem is im not sure how i am supposed to do this, i dont have the answer book.
Here is my code. This prints the two numbers and then does the addition. all the examples in the chapter just use hard coded numbers.
Code:
class baseCalc
{
var $name;
function baseCalc($n)
{
$this->name=$n;
}
function calculate()
{
print "$this->name<br>";
}
}
class addCalc extends baseCalc
{
function calculate()
{
print "$this->name<br>";
}
}
$obj1 = new baseCalc(325);
$obj2 = new baseCalc(45);
$obj3 = new addCalc(325+45);
$obj1->calculate();
$obj2->calculate();
$obj3->calculate();
Does this make sense? So far i have read about IF, FOREACH, ARRAYS, FUNCTIONS, and VARIABLES. It just seems kind of stupid to write all this code to do something that could be done in a few lines.
-Dave