re: I Want Scalar Objects in PHP VIEW POST

FULL DISCUSSION
 

i'd like better and consistent treatment for callables
examples of what we can not do ATM:

array_map(class::method, $array);
//
$o = new stdClass;
$o->f = function($s) {echo "Hello $s";};
$o->f("dev.to");
PHP Warning:  Uncaught Error: Call to undefined method stdClass::f() in php shell code:1

also

  • Closure as constants
  • method toArray for SplTypes : Queue, DoublyLinkedList, etc
  • a Map datatype that is not the actual array
  • a way to programmaticaly define class constants in __construct (for int types)
  • a way to hide class properties from being listed by get_object_vars or a a method that dont list privates.

Just what's in my head ATM :)

 

In the example you give with the callable, it would create conflict between properties and methods of classes, that are totally separated in PHP.
This will create backward compatibility problems.

 

There would be a conflict only if no policy is set up.
What seems obvious to me is to check for declared methods first.
Like you cannot "create" a class identifier that has already being declared in class.
There's already a check to prevent from using an name that is declared "private" for example.
That's the "identity" of the class.
Then if someone declares a property with same name a warning should be raised (or another error policy) because after all good practices : that doesnt' make sense to have a property named as an action or a method named as a noun.

So a simple check to put in place when invoking a class member, priority :

  • first class methods if it exists
  • then check property existence AND that is a callable

This avoids overwriting declared methods while giving the possibility to construct methods dynamically or just use callable as a valid value for a property without aliasing it prior to being able to use it.
Callable as a type must be supported in every part of the language.

Note that example show an anonymous function but it can be any object with magic "__invoke" that is supposedly a valid type for an property.
Valid type but not fully handled ATM.

and for another suggestion since i found a way to declared constant dynamically:
a way to get constants declared in a specific namespace.
something like:

// returns an array with name => value (as get_defined_constants does)
get_namespace_constants(__NAMESPACE__);

Agree with you on those points. An update can be done on this features.
And of course, it would make sense as you say, because of Closure as a type and __invoke method :)
+1 !

code of conduct - report abuse