Skip to main content
Inspiring
December 5, 2010
Answered

PHP class constructor?

  • December 5, 2010
  • 1 reply
  • 744 views

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 ?

This topic has been closed for replies.
Correct answer David_Powers

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.

1 reply

David_Powers
David_PowersCorrect answer
Inspiring
December 5, 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.

Inspiring
December 5, 2010

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