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) Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
harbansb Posted October 11, 2008 Author Share Posted October 11, 2008 Sorry 5.2.4 Quote Link to comment 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. Quote Link to comment 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); Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 ;. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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. 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.