开发者

Using this-> syntax in PHP to access non-static properties

I am new to PHP and am trying to write a very simple class:

<?php
    class Course {
        private $credits;
        public function getCredits() {
        return $this->credits;
       开发者_开发知识库 }
    }   
?>

the problem is when I load this in a browser, I see:

credits; } } ?>

so it looks like the browser is echoing all that follows "this->" - is there something I need to configure in php.ini? thanks for any help!


You can't just load the .php file directly into your browser through the open dialog.

The reason you get that result is

<?php class Course { private $credits; public function getCredits() { return $this->credits; } }
?>

Is being interpreted as a single (invalid) html tag

<?php class Course { ... $this->

Then the rest of the stuff is assumed to be plain text.

The php stuff needs to be parsed by the php interpreter before loaded to the browser. This is usually done through running a web server with php support - like Apache.

Checkout XAMPP for a quick way to get started with this

XAMPP - http://www.apachefriends.org/en/xampp.html


As it turns out, the problem I was having was due to some kind of bug in my installation of XAMPP. The problem shows up only intermittently, and it does look like the ">" is being interpreted as a closing tag. But I could take the same code and run it on a remote web site with no problem. And 50% of the time, it works fine on my own server too. Still curious if anyone else has seen this problem though?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜