php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #9777 Function/Class prototypes?
Submitted: 2001-03-16 00:53 UTC Modified: 2010-12-16 17:39 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: yohgaki at dd dot iij4u dot or dot jp Assigned: yohgaki (profile)
Status: Closed Package: *General Issues
PHP Version: 4.0.4pl1 OS:
Private report: No CVE-ID: None
 [2001-03-16 00:53 UTC] yohgaki at dd dot iij4u dot or dot jp
I think it would be nice to have function/class prototype only for while developing PHP application. (I guess developers are already come up with similar/better idea for it, though)

With prototype, compiler may issue error with invalid parameter or invalid use of function/class and PHP may warn if use of function/class does not match with prototypes at run time.

Such as:
   void function do_something(integer(min, max), resource('file'), array(text,integer), &object('object_name') = null);
   // Such syntax that specifying expected parameters' range/contents may not a good idea, though.
   // The same thing can be done with assert() anyway.
   // It might be good idea enabling assert() in prototype definition.
   // to specify parameters range/contents to get rid of many assert()
   // from function itself. like

   void function($int integer) {
       assert($int > 0);
   }

   // Checking parameter types can be done by assert(). But, it's nice
   // to have prototypes.

   class foo {
        public var pub_var;
        private var priv_var;

        public object foo(integer); 
        protected integer bar(array);
        private void internal_use_only();
   }

and include prototypes with special include statement like

   include_prototype('filename'); // development only prototypes
   require_prototype('filename'); // required prototypes

and have a new php.ini entry for prototype

   prototype.enable = true; // to disable include_prototype

like assert().

Currently, PHP function may take any types for a parameter, even if function accepts certain types. This may result in unwanted behaviors as I submitted a foreach bug. (foreach seems to behave really wrong way, if parameter is not an array.)

Additionally, if prototype may specify function will not return value, parser can prevent programmers to use implicitly returned NULL value as RVALUE.

PHP needs to do additional work for Parse/Compile/Execute with prototypes, but it would be acceptable while developing PHP application. It would be nice to have a ability that programmer can get rid of prototypes for better performance, once she/he is confident with his/her codes.
(Inexperience programmers may write code that is not secure w/o prototypes. But they write insecure code w/ or w/o prototypes anyway)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-10 12:03 UTC] php at thekid dot de
Parts of this have been realized with type hints in PHP5.
 [2010-12-16 17:39 UTC] jani@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues
 [2010-12-16 17:39 UTC] jani@php.net
Abstract stuff is there. Other than that there should not be more complexity added like this.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC