Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Noob help 2

Status
Not open for further replies.

SLMHC

MIS
Jul 23, 2004
274
CA
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.
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
 
I don't think you're giving them what they're asking for.

As I understand the goal of the tutorial, your class should:[ul][li]provide storage for two numbers,[/li][li]provide a class method to insert values into that storage for two numbers[/li][li]provide a class method for displaying the two stored numbers[/li][/ul]Your class does none of these things -- it only provides storage for a single variable.

The child class is, I think, supposed to extend the original class by adding a new class method that adds together the two numbers previously stored and displaying the result of that addition.

You're right -- the exercise does seem a little pointless, as you can do the same thing much easier in non-object-oriented code. This, however, is a "hello world" exercise in OO programming which can teach you some of the basics that I don't think you yet grok.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
My biggest concern is that this question is asking something that i havent been given an example of in the book so far. I tried to take on example they had of entering one number and displaying it, but i dont know how to get two or more numbers to be stored.

I would like to get my head around this because i do see the value of OO programming, i just dont think hte author did a good enough job.

Up to this point in the book the author provided lots of examples and the end of chapter program was a more advanced script of some ideas he had talked about. He also had lots of comments in his code to show what was what, which is missing in this chapter.

-Dave
 
I think ive got the 1st part nailed, storing 2 numbers.
Code:
class BaseCalc {
	var $Number;
	function BaseCalc($number)
		{
		$this->Number = $number;
		}
}
$add_num = new BaseCalc("345");
$add_num2 = new BaseCalc("385");
print "1st Number: $add_num->Number";
print "<br>";
print "2nd Number: $add_num2->Number";

Adding the two numbers has me scratching my head in frustration.

Any hints?


-Dave
 
I suppose creating a class that can store one variable and using it twice is one possible interpretation of the exercise. I would have, however, created a class that has the storage for two values simultaneously and used it once. My way, I think, will make it easier to implement the child class.

I also would not simply reference the class variables from outside the class ("$add_num->Number"). I would have written a method for the class that either outputs the number or returns the number so that script code external to the object could print the numbers. Heck, write both methods.

The whole reason for object-oriented programming is encapsulation, so that a chunkc of code can be as reusable and portable as possible. An object should be a self-contained thing, so it should include code to modify all internal variable storage. A lot of OO languages, including PHP 5, allow a programmer to protect an object's variables so that the only way to manipulate the internal variable storage of the class is through the class' methods. Something like "$add_num->Number" would fail.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
If you dont mind, could you show me how you would do this? I did some googling last night and could only find basic how to's.

I would really like to understand this concept.

-Dave
 
You're on the right track with what you've created. Your class has one internal variable -- give it two.

Your class has a constructor method. Create a regular method that returns the value from your object.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
:eek:}

sleipnir: I can see that you don't want to put some piece of code in order to make Dave think... that's good anyway, but I can see that Dave is pretty lost. I would like to put some piece of code for Dave in order to clarify about objects... do you agree? at least the first class.
 
class BaseCalc {
var $Number; //1st variable
var $Number2; //2nd variable

function BaseCalc($Number, $Number2)
{
$this->Number = $Number;
$this->Number = $Number2; /*not sure if this is what is needed here*/
}
}

I am lost, i though that "function BaseCalc()" was a method.

-Dave
 
IMHO

The activity goes like this: Create a class that stores 2 numbers, display the numbers.

what's about this:

Code:
class BaseCalc {
  var $x;
  var $y;
  
  function BaseCalc($a,$b){
    $this->x=$a;
    $this->y=$b;
  }
  
  function display(){
    echo "Numbers are: " . $this->x . " and " $this->y;
  }
}

$pair = new BaseCalc(10,20);
$pair->display();


Result:
Numbers are: 10 and 20

Ok, this is the first class, now you should do the second class (the extended one).

This class could be with an array variable, but only for this example it is with 2 hardcoded variables. So the extended class should addition these 2 variables and print the result.

Cheers.
 
hmmm.. you post while I was writting the code... you did it good!! only one change to your code:

$this->Number = $Number2;

should be

$this->Number[red]2[/red] = $Number2;

Cheers.
 
i dont know why i didnt think to make a new function to display the numbers...i think i was so frustrated i developed a mental block, or im just mental!

ill putter around with this again tonight.

thanks for the help so far :)

-Dave
 
Chacalinc,

I had to change your code:
Code:
    echo "Numbers are: " . $this->x . " and " $this->y;

to:
Code:
    echo "The numbers are $this->x and $this->y<br>";

Now the display numbers works fine.

Next task, add the two numbers.

I am using the following code:
Code:
class AddCalc extends BaseCalc {

  var $z;

  function AddCalc($c) {
    $this->z=$c;
  }

  function DisplayAddCalc() {
    $this->z = $this->x + $this->y;
    echo "$this->x + $this->y = $this->z";
  }
}
$addition = new AddCalc();
$addition->DisplayAddCalc();

It seems $this->x & $this->y are not being passed to the extened class. Is it my code or is this normal behavour?

-Dave
 
original said:
The activity goes like this: Create a class that stores 2 numbers, display the numbers.

Ok, this part is done, cool!

original said:
Then 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.

Let's see...

[tt]class AddCalc extends BaseCalc { [/tt] :eek:} good!

[tt] var $z;[/u][/tt] :eek:| why you need another variable? you just need to add the inherited variables from the first class.

[tt]
function AddCalc($c) {
$this->z=$c;
}

function DisplayAddCalc() {
$this->z = $this->x + $this->y;
echo "$this->x + $this->y = $this->z";
}
}
[/tt]

:eek:| I would change these functions in only one.

[tt]$addition = new AddCalc();
$addition->DisplayAddCalc();[/tt]
It seems $this->x & $this->y are not being passed to the extened class.


No, the problem here is that you are not assigning a value to the variables. So the code should be something like this:

Code:
class AddCalc extends BaseCalc {

  function AddCalc($a,$b) {
    $this->BaseCalc($a,$b);
  }

  function DisplayAddCalc() {
    $z = $this->x + $this->y;
    echo "$this->x + $this->y = $z";
  }
}
$addition = new AddCalc(10,20);
$addition->DisplayAddCalc();

Anyway, the documentation in the is very good. I recommend you to read it. Regarding OOP:
 
one question, is there a way to reference the original numbers?

-Dave
 
[tt]$addition = new AddCalc(10,20);[/tt]

these are the original numbers. When you code these lines:

[tt]
$pair = new BaseCalc(1,2);
$addition = new AddCalc(10,20);
[/tt]

you have actually two objects, and not only one, so 1 and 2 are the values for the object $pair and 10 and 20 are the original values for object $addition.

Cheers.
 
thanks, i figured it out...instead of extending the class just add the functions to the original class...this is more of what i would like to do in the future.

Here is what i came up with:
Code:
class BaseCalc {

  var $x;
  var $y;

  function BaseCalc($a,$b) {	
    $this->x=$a;
    $this->y=$b;
  }
	
  function Display() {
    echo "The numbers are $this->x and $this->y<br>";
  }

  function DisplayAddCalc() {
    $z = $this->x + $this->y;
    echo "$this->x + $this->y = $z<br>";
  }

  function DisplaySubCalc() {
    $z = $this->x - $this->y;
    echo "$this->x - $this->y = $z<br>";
  }

}

$pair = new BaseCalc(45,244);
$pair->Display();
$pair->DisplayAddCalc();
$pair->DisplaySubCalc();

-Dave
 
instead of extending the class just add the functions to the original class...this is more of what i would like to do in the future.

keep in mind that these 2 objects are a "Hello World" excercise. extended classes are quite usefull when you have several objects with a common root.

cheers.
 
thanks for all the help with this chapter...im still no expert but i hope i know a lil more than i did on monday.

your votes are in the mail.

-Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top