Jump to content

easy CLASS question please help!!!


adam1984

Recommended Posts

Here is my exercise:

 

Create a class called baseCalc() that stores two numbers as properties. Give it a calculate() method that prints the numbers to the browsers.

 

Alright, so that's it! Here is my code so far:

<?php

 

class baseCalc {

  //Properties

  public $number1 = 20;

  public $number2 = 80;

 

 

  //Method

  public function calculate($number1, $number2) {

  print $number1;

  print $number2;

  }

}

 

calculate();

?>

 

When I run this in the browser, I get an error message. It doesn't like when I call the calcuate() function/method. What am I doing wrong?

 

Sincerely,

ad Man

Link to comment
Share on other sites

Haha, no I'm sorry. I really don't have a shitty attitude towards your response. Every time I've posted on here I've said thanks and been thankful for any and all help I've gotten with PHP. I guess I'm just confused on where to put the code. I'm just trying to understand!!! Please reconsider.

Link to comment
Share on other sites

riiiiight.  So if this "exercise" is in some book you picked up, then it would also provide the answer. Flip the page, go to the index, blahblah... unless you know, you're lying about it. 

 

I'll let you in on a little secret:  You'll probably more likely get what you want if you are

 

a) honest about the situation.  Believe it or not, you are not the 1st person to post homework questions.  Or the thousandth.

 

b) willing to actually put some effort into it.  we aren't here to do your (home)work for you. "please post the entire code I need" is not acceptable around here.

 

"Hi, I have this homework assignment and here's what I've tried so far and I can't seem to get it to work, can someone point me in the right direction?"  Is acceptable.  Which is what you started out with, but quickly went downhill from there.

 

I suggest you read the tutorial link I gave you.

Link to comment
Share on other sites

No probs...I know, you're frustrated, we've all been there.

 

Right, after you create a class, create a new instance straight away.

 

Like so:

 

class baseCalc {

  //Properties

  public $number1 = 20;

  public $number2 = 80;

 

 

  //Method

  public function calculate($number1, $number2) {

  print $number1;

  print $number2;

  }

}

 

$baseCalc = new baseCalc;

 

---then to call your function calculate() within that class, you use:

 

$baseCalc->calculate();

 

Kudos to you for picking up a book and teaching yourself. Truly hope that helps.

Link to comment
Share on other sites

Anti-Moronic,

 

Thank you very much. I appreciate the help, honestly.

 

For the record, since Violent whoever does not believe me, the book I have is SAMS TEACH YOURSELF PHP by Matt Zandstra. I'm about half way through it and this chapter introduces objects and classes, methods, constructs, etc.

 

I'm very honest, and have been straight up about every post I've ever written on here. So again, Anti-Moronic, thank you very much for the help.

 

SOLVED.

Link to comment
Share on other sites

SOLVED.

 

No it's not. You haven't stored anything as a property as the exercise tells you to do. I'll leave the task of figuring out what a property is and how you use it to you.

 

Edit: Well, you have, but you aren't using the properties for anything.

Link to comment
Share on other sites

Hey Asshole,

 

I don't have a teacher, or professor, I'm not taking any class. I picked up a book from the store and am trying to learn. I read the chapter, and am having a little trouble with this exercise.

before i see this i wanted to make you an example how should you do the class instantiate and use it ;) but since your language is not appropriate with the rules here and the respect for others.. continue breaking nuts cuz all you gonna do is make everyone angry the first response you had the guy told ya that first you have to access the class(instantiate) and hold it under a variable it was nothing wrong your answer it had to be quite simple i`m sorry i tried that and didn`t work.

 

ohh for the record sams teach you php doesn`t contain anything about OOP it teaches you only php and if would read a book about OOP the first thing it teaches you is how to create your first class return the simple Hello World and instantiate..

it makes you three times a liar and that not a lucky number.. maybe you should first apologize to the guys here who took 5 minutes from their time to read your post and try to help ya out;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.