adam1984 Posted May 25, 2009 Share Posted May 25, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/159618-easy-class-question-please-help/ Share on other sites More sharing options...
Anti-Moronic Posted May 25, 2009 Share Posted May 25, 2009 You have to create a new instance: $baseCalc = new baseCalc; $baseCalc->calculate(); Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/159618-easy-class-question-please-help/#findComment-841864 Share on other sites More sharing options...
adam1984 Posted May 25, 2009 Author Share Posted May 25, 2009 No I guess that didn't help me out too much? Where do I put that code? Are you willing to post the entire code I need inside my php tags? Quote Link to comment https://forums.phpfreaks.com/topic/159618-easy-class-question-please-help/#findComment-841866 Share on other sites More sharing options...
Anti-Moronic Posted May 25, 2009 Share Posted May 25, 2009 With a shitty attitude like that, no. Do it yourself. Quote Link to comment https://forums.phpfreaks.com/topic/159618-easy-class-question-please-help/#findComment-841869 Share on other sites More sharing options...
.josh Posted May 25, 2009 Share Posted May 25, 2009 *sniffsniff* I smell homework. Quote Link to comment https://forums.phpfreaks.com/topic/159618-easy-class-question-please-help/#findComment-841871 Share on other sites More sharing options...
adam1984 Posted May 25, 2009 Author Share Posted May 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/159618-easy-class-question-please-help/#findComment-841873 Share on other sites More sharing options...
.josh Posted May 25, 2009 Share Posted May 25, 2009 It looks like you need to learn the basics of OOP in the first place. Maybe you should have paid attention when your teacher was talking to you about it. http://www.phpfreaks.com/tutorial/oo-php-part-1-oop-in-full-effect/ Quote Link to comment https://forums.phpfreaks.com/topic/159618-easy-class-question-please-help/#findComment-841877 Share on other sites More sharing options...
adam1984 Posted May 25, 2009 Author Share Posted May 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/159618-easy-class-question-please-help/#findComment-841880 Share on other sites More sharing options...
.josh Posted May 25, 2009 Share Posted May 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/159618-easy-class-question-please-help/#findComment-841883 Share on other sites More sharing options...
Anti-Moronic Posted May 25, 2009 Share Posted May 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/159618-easy-class-question-please-help/#findComment-841884 Share on other sites More sharing options...
Anti-Moronic Posted May 25, 2009 Share Posted May 25, 2009 Crayon Violet makes an interesting point...do you deserve kudos? Quote Link to comment https://forums.phpfreaks.com/topic/159618-easy-class-question-please-help/#findComment-841888 Share on other sites More sharing options...
adam1984 Posted May 25, 2009 Author Share Posted May 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/159618-easy-class-question-please-help/#findComment-841891 Share on other sites More sharing options...
adam1984 Posted May 25, 2009 Author Share Posted May 25, 2009 Crayon-Violent, although I'm sure knows MUCH more about PHP than myself. Doesn't know anything about me, personally. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/159618-easy-class-question-please-help/#findComment-841893 Share on other sites More sharing options...
Daniel0 Posted May 25, 2009 Share Posted May 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/159618-easy-class-question-please-help/#findComment-841897 Share on other sites More sharing options...
.josh Posted May 25, 2009 Share Posted May 25, 2009 personally I also sort of assumed that calculate() was also supposed to draw on the properties to do its thing, as well. But you know what they say about assuming Quote Link to comment https://forums.phpfreaks.com/topic/159618-easy-class-question-please-help/#findComment-841899 Share on other sites More sharing options...
adam1984 Posted May 25, 2009 Author Share Posted May 25, 2009 Well I'll keep on grinding at it then. Quote Link to comment https://forums.phpfreaks.com/topic/159618-easy-class-question-please-help/#findComment-841901 Share on other sites More sharing options...
fantomel Posted May 25, 2009 Share Posted May 25, 2009 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;) Quote Link to comment https://forums.phpfreaks.com/topic/159618-easy-class-question-please-help/#findComment-841923 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.