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

PHP class constructor?

Engaged ,
Dec 05, 2010 Dec 05, 2010

Copy link to clipboard

Copied

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

Views

661
Translate

Report

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.

Votes

Translate
LEGEND ,
Dec 05, 2010 Dec 05, 2010

Copy link to clipboard

Copied

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.

Votes

Translate

Report

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

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Report

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