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?

3

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Magic functions in what context? In PHP? Or a convention in particular frameworks like WordPress or Laravel?
WordPress uses naked double underscores ("__"): Double underscore in PHP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.