PHP Class Woes

Associate
Joined
9 Nov 2005
Posts
767
Location
places..
Hello Peeps,

I need some PHP help! I seem to get some kind of syntactical error, with a class, I have "paraphrased" the code here, and replicated the problem.

The Classfile.

PHP:
<?php

class MyClass {
var $number
function biggerthanfive() {
    if ($this->number >= 5) {
        return (true);
    }else{
        return (false);
    }
}
}

?>
Which is included in this script:

PHP:
<?php
    include "newClass.php";
    $myclass = new MyClass;
    $myclass->number = 8;
    $biggerthanfive = $myclass->biggerthanfive();
    
    if(!$biggerthanfive) {
        echo "number is lager than 5";
    } else {
        echo "number is not larger than 5";
    }

?>
I can't see anything really wrong with this, the biggerthanfive() function works fine as a standalone function, so I think its something to do with the contstructor? Noy only does it throws an error when I run it, but netbeans tells mea there is something wrong too :( I'm stumped, I have 2 Internets to give away to someone who can help put me in the right direction!

Many Thanks

Edit: the problem seems to be with the function in the class.

 
There's a lot of superfluous code in there, but other than that I can't see any breaking errors.

What error are you actually getting? What is the actual code you are using?
 
The class method could be shortened to:

PHP:
<?php

class MyClass {

  var $number

  function biggerthanfive() {
    return $this->number > 5;
  }
}
?>

Subliminal Aura spotted the copy and pasting mistake, should be greater than (>) not greater than or equal to (>=).
 
Last edited:
Back
Top Bottom