0

I was just reading some code and I came across a function named __toString. Here is the function prototype:

public function __construct($par)

What is the reason the coder chose __construct and not construct as the function name?

I have read this question which says that private and protected functions can start with an underscore, but it is a public function. Why has the coder chosen to use underscore in this case?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 3
    possible duplicate of [Why double underscore (__) in php?](http://stackoverflow.com/questions/5348663/why-double-underscore-in-php), see also: http://php.net/manual/en/language.oop5.magic.php – Wesley Murch Mar 14 '12 at 09:11
  • "PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality." – Gordon Mar 14 '12 at 09:27
  • Possible duplicate of *[Why double underscore (\_\_) in PHP function names?](https://stackoverflow.com/questions/5348663/why-double-underscore-in-php-function-names)*. – Peter Mortensen Apr 20 '22 at 18:23

1 Answers1

2

__construct and __toString are all magic functions that start with a double _.

And all private functions start with a single _ that is not standard, but developers prefer this way to identify public and private methods...

And all magic function are not public.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sandeep
  • 2,244
  • 1
  • 23
  • 38
  • Magic functions in what context? In PHP? Or a *convention* in particular frameworks like [WordPress](https://en.wikipedia.org/wiki/WordPress) or [Laravel](https://en.wikipedia.org/wiki/Laravel)? – Peter Mortensen Apr 20 '22 at 18:21
  • WordPress uses *naked* double underscores ("__"): *[Double underscore in PHP](https://stackoverflow.com/questions/1777131/double-underscore-in-php/1777147#1777147)* – Peter Mortensen Apr 20 '22 at 18:26