otuatail Posted August 12, 2009 Share Posted August 12, 2009 Hi. I have never used classes in php before. I tried an example from a website that worked. however I added a second one of mine and it dosn't work. Any ideas? <?php class MyClass { var $email; // use a function without variables function check_email(){ if(ereg("^.+@.+\..+$", $this->email)) return (true); else return (false); } // Use a function with variables function image_strip($somehtml){ $somehtml = preg_replace("/(<img)(.*?)(>)/si", "", $somehtml); return $somehtml; } } class MyCube { // var $value; var $result; function calculate() { $result = $value * $value * $value; return $result; } } ?> <?php include "clsMyClass.php"; $myclass = &New MyClass; $myclass->email = "you@somewhere.com"; $check_email = $myclass->check_email(); if(!$check_email){ echo "The email address is not valid!"; } else { echo "The email address is valid!"; } $MyCalc = &New MyCube; $MyCalc ->value = 4; $Return = calculate(); echo "Value = " . $Return; ?> Thanks. Quote Link to comment Share on other sites More sharing options...
br3nn4n Posted August 12, 2009 Share Posted August 12, 2009 Alright, what error are you getting? Also when you are creating new objects the syntax is "new objectname" without the uppercase n and the & sign. See if that helps anything! Quote Link to comment Share on other sites More sharing options...
otuatail Posted August 12, 2009 Author Share Posted August 12, 2009 I'm Not getting any error. I just get The email address is valid! I should get The email address is valid! Value = 64 The first example came from a websit with $myclass = &New MyClass; and it worked. http://www.des-otoole.co.uk/class/ Quote Link to comment Share on other sites More sharing options...
waterssaz Posted August 12, 2009 Share Posted August 12, 2009 well you should get an error ;-) you are not calling the class function properly at all. should be: $MyCalc = &New MyCube; $MyCalc ->value = 4; $Return = $MyCalc->calculate(); echo "Value = " . $Return; Quote Link to comment Share on other sites More sharing options...
waterssaz Posted August 12, 2009 Share Posted August 12, 2009 Also in your class class MyCube { // var $value; var $result; function calculate() { $result = $value * $value * $value; return $result; } } should be class MyCube { // var $value; var $result; function calculate() { $result = $this->value * $this->value * $this->value; return $result; } } Quote Link to comment Share on other sites More sharing options...
otuatail Posted August 12, 2009 Author Share Posted August 12, 2009 Thanks for that it works now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.