Jump to content

PHP Classes won't run


harbansb

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.