Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

PHP class constructor?

Engaged ,
Dec 05, 2010 Dec 05, 2010

I'm useed to object orientated programming (C#) so now I'n switching to php its time to write classes. But there's something I don't get in the following code:

<?php
Class path{

    private $serverPath;
   
   
    function _construct()
    {
        $this->serverPath = $_SERVER['DOCUMENT_ROOT'];
       
               
    }
       
       
    public function getServerpath()
    {
        return $this->serverPath;
    }
   
    public function rel2abs ($relpath)
    {
        if (substr($relpath,0) != "/") $relpath = "/".$relpath;
        $relpath = $this->serverPath .$relpath;
        return realpath($relpath);   
    }

}
?>

After debugging it appears that after the object is constructed the private value $serverPath remains NULL ?

TOPICS
Server side applications
664
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Dec 05, 2010 Dec 05, 2010

M.R.Biesheuvel wrote:

After debugging it appears that after the object is constructed the private value $serverPath remains NULL ?

That's because of an error in your code. The constructor method in a PHP class begins with TWO underscores.

function __construct()

In your code, there's only one underscore.

Translate
LEGEND ,
Dec 05, 2010 Dec 05, 2010

M.R.Biesheuvel wrote:

After debugging it appears that after the object is constructed the private value $serverPath remains NULL ?

That's because of an error in your code. The constructor method in a PHP class begins with TWO underscores.

function __construct()

In your code, there's only one underscore.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 05, 2010 Dec 05, 2010
LATEST

Thanks, I did not see it in the examples i took __ for _

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines