Hi,

I've done a bit of refactoring work around code using "zend_result",
but I keep wondering why it even exists.

It was added in 1999 by commit 573b46022c46 in a huge 16k line commit
(as macros, converted to enum in 2012 by commit e3ef84c59bf).

In 1999, C99 was brand new, and the "bool" type had just been
introduced to the C language - yes, C was 18 years old when a native
boolean type was added - but PHP didn't switch to C99 for another 19
years later (b51a99ae358b).

C had a long history of abusing "int" as boolean type, wasting 2 or 4
bytes for something that could have easily fit in 1 byte.  As if that
wasn't obscure enough, POSIX used 0 for success and -1 for error (and
missed the chance to return error codes, and instead added a global
variable for that which caused more headaches).  (And don't get me
started on the horrible strcmp() return value.)  Returning errors in C
is a huge obscure and inconsistent mess; every function does it
differently.

This is PHP's original definition:

 #define SUCCESS 0
 #define FAILURE -1 /* this MUST stay a negative number, or it may effect 
functions! */

This appears to follow the POSIX school of bad error return values.
There's a comment which makes the thing even more obscure.

Really, why does it need to be negative?

Why does it imitate POSIX instead of using a boolean? (i.e. "int" and
non-zero for success for old-schoolers)

The obvious way to indicate success or failure (without giving details
about the nature of the failure) would be to just return "bool".  With
C99, any discussion on "0 or 1" vs "-1 or 0" is obsolete - there is
now a canonical boolean type that should be used.

Of course, I know already that getting rid of "zend_result" in favor
of "bool" would be a major API breakage that requires careful
refactoring of almost all extensions.

I just want to understand why "zend_result" was ever invented and why
it uses those surprising integer values.  The commit message and that
code comment doesn't explain it.

Rephrased: do you consider it a worthy goal to eventually get rid of
"zend_result", or do you believe it's good API design that should stay
forever?

(Yet again I wish PHP was fully C++ - unlike C, C++ has strongly-typed
enums which cannot be casted implicitly to "int" or "bool"; that makes
refactoring a lot easier and safer.)

Max

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to