Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Thanked 0 Times in 0 Posts

    BEGINNER - Help me convert functions to a class !

    Hello people,

    can anyone help me and add a class into my code and put all the functions into that class i am stuck on this step all works but I don't know how to put my functions into a class...

    here is my code:
    PHP Code:
       <title> Calc </title> 
    hello() {
    "<p>Welcome to my web site</p>";
    printBreak($text) {
    addNumbers() {



    addNumbers1() {



    printBreak("This is a line");
    $y" - SUM <br/>";
    $a" - SUB <br/>";
    // for loop
    $total 0;
     for (
    $y $y<=100$y++) {
    $total $total $y;
    $total " - For Loop <br/>";
    $sum 0;
    $sum += $a;
    $sum " - While Loop<br/>"
     if (
    $total $sum) {
    "for loop is bigger than while loop";
     } elseif (
    $total == $sum) {
    "for loop is equal to while loop";
     } else {
    "for loop is smaller than while loop";

  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,668 Times in 2,637 Posts
    Using a class isn't for grouping things together, OOP is a completely different paradigm than procedural. If your not able to convert code to a class, than that implies that you're not familiar with the paradigm of OOP, and therefore there is no reason to convert the code.
    Writing a flexible calculator with OOP would require at least one class to serve as the base for all computations (the execution entry point), and an interface for the calculations. The interface would be used for implementing classes responsible for operating on terms, and would have one for each operation available. That would be a flexible calculator. For a simple calculator that still relies heavily on procedural code, than you can simply write a class with static operations within it and use the procedural code to figure out what to do with it.

    Or, in procedural world, you simply add another function (optional), and use an else if to determine what to do.

    Also, you should break that habit of using global keyword and use proper parameter handling. Global is too difficult to keep track of (ie: its impossible to keep track of) and is an absolute nightmare in OOP. Use it only if there is a signature which the method is bound to and doesn't include all of the data you need to process it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts