PHP global function and class with the same name

This works in PHP:

class Foo{
  ....
}

function Foo(){return new Foo();}

Is this a safe / wise practice in PHP? (in the sense that this may cause problems now or in future versions)

The reason for using this is to somehow make the code more understandable this way:

class Foo{
   var $bar,$hold;
   function setBar($how=true){$bar=$how;return $this;}
   function setHold($how=true){$hold=$how;return $this;}
}
function Foo(){return new Foo();}
//------------------

$someFoo=Foo()->setBar()->setHold(false);
// instead of $someFoo=(new Foo())->setBar()->setHold(false);

which could make the code more readable and / or preserve some typing.

+4
source share

Source: https://habr.com/ru/post/1655205/


All Articles