Probably the most basic PHP problem you'll ever have to answer [help]

Discussion in 'PHP' started by scottlpool2003, Oct 11, 2012.

  1. #1
    I'm brand spanking new to objects/classes and starting from the ground up (after using procedural code for a good few years).

    I'm just testing out basic input/output using an IF statement to get used to it all...

    <?php
    
    //This would be stored in a class file
    
    class Testclass
    {
     public $testvarb = "Signed In";
     public $testvar = "Not signed in";
    
    
     function dosomething()
     {
      echo $this->testvar;
     }
    
     function dosomethingb()
     {
      echo $this->testvarb;
     }
    
    
    }
    
    //This would be called out on the page
    
    //Check if user is logged in
    
    $Testclass = new Testclass();
    
    
    $loggedin = "0";
    
    if ($loggedin == "1") {
    $Testclass->dosomething();
    }
    
    else {
    $Testclass->dosomethingb();
    }
    
    
     
    
    ?>
    PHP:
    Its outputting backwards... 0 outputs "Signed In" whereas 1 outputs "Not Signed In"

    Please don't fix the code, just advise me what is wrong.

    Thanks.
     
    scottlpool2003, Oct 11, 2012 IP
  2. kevinn13

    kevinn13 Greenhorn

    Messages:
    24
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    11
    #2
    $loggedin = "0";

    if ($loggedin == "1")
    {
    $Testclass->dosomething(); // Not used
    }
    else {
    $Testclass->dosomethingb(); // calls testvarb = "Signed In";

    }

    Here is the other way

    $loggedin = "1";

    if ($loggedin == "1")
    {
    $Testclass->dosomething(); // $testvar = "Not signed in";
    }
    else {
    $Testclass->dosomethingb(); // Not used
    }


    So you need to change this line ($loggedin == "1") to ($loggedin == "0")
     
    kevinn13, Oct 11, 2012 IP
  3. broxen

    broxen Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    class properties is encapsulated, you need getter/setter (ask google for this) and class property which hold current login state.
    also, use boolean comparison,

    a complete fix here:
    http://pastie.org/5038325
    not directly added by your request
     
    broxen, Oct 11, 2012 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    I think you were thinking backwards when you named the two variables at the top of the class. You have $testvarb first but "Signed In" first. Didn't you mean $testvar = "Signed In" and $testvarb = "Not Signed In"?
     
    Rukbat, Oct 14, 2012 IP
  5. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #5
    Thanks for your help everyone, yes silly mistake to make.
     
    scottlpool2003, Oct 15, 2012 IP