Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    1
    Thanks
    0
    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:
          <html>
       <head> 
       <title> Calc </title> 
       </head> 
       <body> 
       <?php 
       $x
    =4.5;
       
    $y=5.5;
       
    $a=20;
      
    $b=10;
      
     
     function 
    hello() {
         print 
    "<h1>HELLO!</h1>";
         print 
    "<p>Welcome to my web site</p>";
     }
     
     function 
    printBreak($text) {
         print 
    "$text<br>";
     }
      
     function 
    addNumbers() {
          global 
    $x,$y;
     

        

        

    $y=$x+$y;
     }
     function 
    addNumbers1() {
          global 
    $a,$b;
     

        

        

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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 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
    •