I'm trying to get basic information based on the definition of class and use the calculation < P> Code here
& lt ;? Php class calculator {var $ number1 = 4; Var $ number2 = 5; Add function ($ a, $ b) {$ c = $ a + $ b; Print ("The sum of your numbers: $ c"); Print ($ c); }} $ Cal = new calculator; $ Cal- & gt; Add ($ number 1, $ number 2); ? & Gt; What's visible in my browser:
The sum of your numbers: 0
why not 9 < What are the values of Code> $ number1 and $ number2?
Are you passing? $ number1 and $ number2 as $ cal-> Number1 and $ cal- & gt; Number2 is not the same as . You are defining the two properties of an object, and passing two separate, different variables in the function of the class. You are basically two pairs of numbers - with a pair of objects, values of 4 and 5, and there is no value outside the function (both 0) that you are adding.
You can try this:
Or this:
& lt ;? Php class calculator {Private $ number1 = 4; Private $ number2 = 5; Add function () {$ c = $ this- & gt; Number 1 + $ this- & gt; Number 2; Print ("The sum of your numbers: $ c"); Print ($ c); }} $ Cal = new calculator; $ Cal- & gt; Add (); Or this:
& lt ;? Php class calculator {add function ($ a, $ b) {$ c = $ a + $ b; Print ("The sum of your numbers: $ c"); Print ($ c); }} $ Cal = new calculator; $ Cal- & gt; Add (4, 5);
Comments
Post a Comment