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 = "[email protected]"; $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. Link to comment https://forums.phpfreaks.com/topic/169914-solved-using-classes/ 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! Link to comment https://forums.phpfreaks.com/topic/169914-solved-using-classes/#findComment-896368 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/ Link to comment https://forums.phpfreaks.com/topic/169914-solved-using-classes/#findComment-896372 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; Link to comment https://forums.phpfreaks.com/topic/169914-solved-using-classes/#findComment-896387 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; } } Link to comment https://forums.phpfreaks.com/topic/169914-solved-using-classes/#findComment-896390 Share on other sites More sharing options...
otuatail Posted August 12, 2009 Author Share Posted August 12, 2009 Thanks for that it works now. Link to comment https://forums.phpfreaks.com/topic/169914-solved-using-classes/#findComment-896396 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.