harbansb Posted October 10, 2008 Share Posted October 10, 2008 Hello, I would like to start of by saying that I am fairly new to php, and would greatly appreciate any help. I am using PHP 5.4.2. When I write a class nothing is displayed, for example : -------student.php --------- <?php class Student { private $name; public function __construct( $name ) { $this->name = $name; } public function getName() { return $this->name; } } $s = new Student('Mary'); echo( $s->getName() ); ?> Furthermore, if I try to echo a statement below the class definition it does not display anything, php before a class definition runs fine or on a separate file runs fine. For example: -------student.php --------- <?php class Student { private $name; public function __construct( $name ) { $this->name = $name; } public function getName() { return $this->name; } } echo( "Hello World" ); ?> --------------------------- Nothing is displayed. Maybe I have to change the php.ini file? Any help would be greatly appreciated. Thank You. (edited by kenrbnsn to add tags) Link to comment https://forums.phpfreaks.com/topic/127926-php-classes-wont-run/ Share on other sites More sharing options...
DarkWater Posted October 10, 2008 Share Posted October 10, 2008 The examples posted work fine for me. o_O Link to comment https://forums.phpfreaks.com/topic/127926-php-classes-wont-run/#findComment-662359 Share on other sites More sharing options...
PFMaBiSmAd Posted October 11, 2008 Share Posted October 11, 2008 Since php 5.4.x does not exist, that might be the problem. Link to comment https://forums.phpfreaks.com/topic/127926-php-classes-wont-run/#findComment-662374 Share on other sites More sharing options...
DarkWater Posted October 11, 2008 Share Posted October 11, 2008 Since php 5.4.x does not exist, that might be the problem. LOL I didn't even see that. Link to comment https://forums.phpfreaks.com/topic/127926-php-classes-wont-run/#findComment-662376 Share on other sites More sharing options...
harbansb Posted October 11, 2008 Author Share Posted October 11, 2008 Sorry 5.2.4 Link to comment https://forums.phpfreaks.com/topic/127926-php-classes-wont-run/#findComment-662382 Share on other sites More sharing options...
DarkWater Posted October 11, 2008 Share Posted October 11, 2008 Sorry 5.2.4 I run PHP 5.2.4 as well, and the code executes just fine for me. Link to comment https://forums.phpfreaks.com/topic/127926-php-classes-wont-run/#findComment-662388 Share on other sites More sharing options...
PFMaBiSmAd Posted October 11, 2008 Share Posted October 11, 2008 I'm going to guess a fatal runtime error and you are doing this on a system where error_reporting or display_errors is set to hide the errors that would help you when learning php, developing php code, or debugging php code. Add the following two lines after your <?php tag - ini_set ("display_errors", "1"); error_reporting(E_ALL); Link to comment https://forums.phpfreaks.com/topic/127926-php-classes-wont-run/#findComment-662391 Share on other sites More sharing options...
harbansb Posted October 11, 2008 Author Share Posted October 11, 2008 thanks, I just turned on error reporting, and updated the server to 5.2.6, and still nothing comes up. I've changed the code to <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); class Student {} echo( "Hello World" ); ?> Thanks for the help Link to comment https://forums.phpfreaks.com/topic/127926-php-classes-wont-run/#findComment-662399 Share on other sites More sharing options...
DarkWater Posted October 11, 2008 Share Posted October 11, 2008 I just ran the script and it said "Hello World". Are you sure PHP is up and running on your server? Link to comment https://forums.phpfreaks.com/topic/127926-php-classes-wont-run/#findComment-662400 Share on other sites More sharing options...
harbansb Posted October 11, 2008 Author Share Posted October 11, 2008 yeah, here is a link to a page that just contains <? phpinfo() ?> http://helm.harlemlive.org/phpinfo.php thanks Link to comment https://forums.phpfreaks.com/topic/127926-php-classes-wont-run/#findComment-662404 Share on other sites More sharing options...
harbansb Posted October 11, 2008 Author Share Posted October 11, 2008 If this helps here is the link to the above file http://helm.harlemlive.org/Contacts/classes/index1.php thanks. Link to comment https://forums.phpfreaks.com/topic/127926-php-classes-wont-run/#findComment-662406 Share on other sites More sharing options...
PFMaBiSmAd Posted October 11, 2008 Share Posted October 11, 2008 That exact posted code (in post #7) should generate a fatal parse error due to a missing ; The two lines I posted for adding to your script were based on information from you that an echo statement in the script before the class code produced output. The two settings should be turned on in the php.ini since turning them on in your script won't have an effect if your script never runs. Link to comment https://forums.phpfreaks.com/topic/127926-php-classes-wont-run/#findComment-662408 Share on other sites More sharing options...
DarkWater Posted October 11, 2008 Share Posted October 11, 2008 That exact posted code (in post #7) should generate a fatal parse error due to a missing ; The two lines I posted for adding to your script were based on information from you that an echo statement in the script before the class code produced output. The two settings should be turned on in the php.ini since turning them on in your script won't have an effect if your script never runs. I see no missing ;. Link to comment https://forums.phpfreaks.com/topic/127926-php-classes-wont-run/#findComment-662415 Share on other sites More sharing options...
PFMaBiSmAd Posted October 11, 2008 Share Posted October 11, 2008 I'm probably (not)seeing things that should not be there (I jumped the gun and made that statement about the line with the empty class definition on it.) Edit: His link to the page is now giving a parse error about an {. Based on the url and page name, I don't think the code in the first post was all the relevant code on that page. Link to comment https://forums.phpfreaks.com/topic/127926-php-classes-wont-run/#findComment-662418 Share on other sites More sharing options...
harbansb Posted October 11, 2008 Author Share Posted October 11, 2008 sorry for the confusion I have changed the index1.php to <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); class B{ } echo('hello'); ?> I have also added the below code to the php.ini ini_set ("display_errors", "1"); error_reporting(E_ALL); It is giving the following error: Parse error: syntax error, unexpected '{' in /home/site/helm/Contacts/classes/index1.php on line 5 Thanks Link to comment https://forums.phpfreaks.com/topic/127926-php-classes-wont-run/#findComment-662422 Share on other sites More sharing options...
PFMaBiSmAd Posted October 11, 2008 Share Posted October 11, 2008 As long as the class name is not a reserved php keyword, what you have posted is correct and does work (the code you have posted in the first post and the latest post works for me as well.) Either your php build is broken (OOP is part of the php core and to the best of my knowledge it cannot be turned off or disabled) or there is something about your file that is not right and is not being parsed properly. Link to comment https://forums.phpfreaks.com/topic/127926-php-classes-wont-run/#findComment-662439 Share on other sites More sharing options...
harbansb Posted October 11, 2008 Author Share Posted October 11, 2008 it seems to have corrected itself, thanks for the help. Link to comment https://forums.phpfreaks.com/topic/127926-php-classes-wont-run/#findComment-662447 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.