Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. Hi guys i am trying to make a php calculator class which need to multiple numbers through fucntion like when user try to run like this through commpand line claculator. And it should allow defining what delimiter is used to separate numbers example: calculator.
And it should not accept negative numbers too. And if user pass number as more than it shouwld ignoe and give me the result exmaple php calculator. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Make a php calculator class using oops concepts Ask Question. Asked 3 years, 4 months ago. An object needs both a set of methods and a set of variables that represent its state, and can be used to differentiate instances of the object.
I would suggest trying to make "moody" calculators. A calculator with a happy mood will add 2 to each result, and an angry calculator will subtract 2 from each result. In general, we don't fix value in those kind of class.
Your methods should take their 2 values by arguments instead of picking private members. Thus, Calculator becomes a tool, and we should not need to allocate this kind of object.
That's why Calculators methods should be static. In this way, all you need to call is Calculator::add 1, 2. You can find those kinds of class everywhere. Like Vector, Matrice in math or 3D.
Or write to the output or anything like that. Take a look at this example. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 8 years, 9 months ago. Active 5 years, 2 months ago. Viewed 18k times. Clay 4, 3 3 gold badges 30 30 silver badges 47 47 bronze badges. Basi Manele Basi Manele 97 1 1 gold badge 1 1 silver badge 4 4 bronze badges. Please take a minute to align the code properly.
It is barely possible to read the code when it is all messed up. In fact you're not creating any proper objects at all. It is good to try it for educational purposes, so start google a good articles about OOP.
This task can also be done by using functions or by creating utility class. Let's try utility class approach. For this task it is natural for me to use static methods, so the code will look like this:.
How does this look like? For me code is: readable, well documented, easy to use. Variables has a proper names, comments are everywhere : Good luck in learning!
Im not going to comment on the implementation of the calculator there are more well versed people out there but I will comment on the coding style;. I much prefer I think most people do to return or throw early to avoid more brackets and indentation. There is a lot of resources out there that suggest this is the correct way to pass data to a class.
To me variables passed to a constructor should be other classes this class depends on, the reason is so we don't have to add more manual definitions when using dependency injection this is not true in every instance. First thing that caught my eye was meaningless variable names: x, y, z. Although the class is extremely simple, it wouldn't hurt giving them proper names like operand, operator, etc. Sign up to join this community. The best answers are voted up and rise to the top.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more.
0コメント