php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #75023 File Private Visibility Type
Submitted: 2017-08-02 11:13 UTC Modified: 2017-08-04 22:56 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: php at haravikk dot me Assigned:
Status: Suspended Package: *General Issues
PHP Version: Next Major Version OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
47 - 10 = ?
Subscribe to this entry?

 
 [2017-08-02 11:13 UTC] php at haravikk dot me
Description:
------------
While a lot of classes written for PHP tend towards one class per file, it is often still useful to keep supporting classes within the same file.

For example, say I have an abstract type `Object`, with two concrete variations; `Object_Wrapper` and `Object_Instance`. If these are only obtained through static methods on `Object`, then there is no need to separate these out into their own files, as they should never be called for directly (i.e- all interaction occurs via `Object`).

However, because of PHP's visibility model, interaction between these types may still require public methods that the developer doesn't actually want to be public.

What is really needed is the ability to specify a file-private visibility type, making a method/property/constant visible only to other code/types from the same file. In the example code below, both `Object_Wrapper` and `Object_Instance` have public constructors, but shouldn't really need to, i.e- it would be preferable if these were limited to the same file only, to prevent accidental creation of one of these directly, rather than through `Object` indirectly as designed.

Test script:
---------------
<?php

abstract class Object {
    public static function create() { return new Object_Wrapper(new Object_Instance()); }
}

/** Wraps some useful functionality around an Object_Instance, e.g- if the instance provides some baseline functionality, with wrappers providing more specifics. */
class Object_Wrapper extends Object {
    private $object;
    /** @internal */
    function __construct(Object_Instance $object) { $this->object = $object; }
}

class Object_Instance extends Object {
    /** @internal */
    function __construct() { /* Object is pretty useless right now */ }
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-08-02 11:26 UTC] nikic@php.net
-Package: PHP Language Specification +Package: *General Issues
 [2017-08-04 22:56 UTC] stas@php.net
-Status: Open +Status: Suspended
 [2017-08-04 22:56 UTC] stas@php.net
Thank you for your interest in PHP and for submitting a feature request. Please be aware that due to the magnitude of change this request requires, it would be necessary to discuss it on PHP Internals list (internals@lists.php.net) as an RFC. Please read the guide about creating RFCs here:
https://wiki.php.net/rfc/howto
If you haven't had experience with writing RFCs before, it is advised to seek guidance on the Internals list (http://php.net/mailing-lists.php) and/or solicit help from one of the experienced developers. 

Please to not consider this comment as a negative view on the merits of your proposal - every proposal which requires changes of certain magnitude, even the very successful and widely supported ones, must be done through the RFC process. This helps make the process predictable, transparent and accessible to all developers.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 23:01:28 2024 UTC