SCook Posted January 13, 2007 Share Posted January 13, 2007 I am having trouble with classes. Here's a very simple class I made as a test, and the error message I get:[code]class dog { public function bark() { print("Whoof!"); }}[/code]And when I instantiate the class:$dog = new dog;$dog->bark;and run it, I get this:Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/ow1206/public_html/admin/class.class on line 4Everything seems to be fine, but I just can't get it ro run, even with dot notation, and so on. Any help would be great. Link to comment https://forums.phpfreaks.com/topic/34025-class-not-working/ Share on other sites More sharing options...
kobmat Posted January 13, 2007 Share Posted January 13, 2007 isnt it supposed to be$dog = new dog;$dog->bark(); Link to comment https://forums.phpfreaks.com/topic/34025-class-not-working/#findComment-159951 Share on other sites More sharing options...
utexas_pjm Posted January 13, 2007 Share Posted January 13, 2007 Are you running PHP 4 or 5? PHP 4 doesn't support public / private member data or methods. Also, kobmart is right calling[code]$dog->bark;[/code]Is actually trying to reference member data of class dog named bark instead of the method "bark()".Best,Patrick Link to comment https://forums.phpfreaks.com/topic/34025-class-not-working/#findComment-159953 Share on other sites More sharing options...
SCook Posted January 13, 2007 Author Share Posted January 13, 2007 Yeah, that was it. Stupid php 4 and up. That really throws off my OOP, which I why I really haven't used classes before. Link to comment https://forums.phpfreaks.com/topic/34025-class-not-working/#findComment-159954 Share on other sites More sharing options...
utexas_pjm Posted January 13, 2007 Share Posted January 13, 2007 Don't let PHP4 discourage you from writing OO code. I'd guess that > 90% of the code I've written professionally has been OO PHP4. Your class example can be easily rewritten to work in PHP4:[code]<?phpclass dog { function bark() { print("Whoof!"); }}$dog = new dog();$dog->bark();?>[/code]Best,Patrick Link to comment https://forums.phpfreaks.com/topic/34025-class-not-working/#findComment-159957 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.