php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #61277 Feature request: Shared Keyword
Submitted: 2012-03-04 20:28 UTC Modified: 2012-03-05 15:38 UTC
From: thbley at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.5.0 OS: All
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: thbley at gmail dot com
New email:
PHP Version: OS:

 

 [2012-03-04 20:28 UTC] thbley at gmail dot com
Description:
------------
Feature request: Shared Keyword

Declaring class properties as shared makes them accessible for all instances of a PHP script containing the same class name and the same property name.
This functionality allows a shared memory access similar to apc_store() and apc_fetch().

The shared property implies the static property.
Shared propertes can be accessed without needing an instantiation of the class.
If shared is not set for a property, it will be non-shared by default.
Shared properties are accessed through the class name or a class name variable or "self", and the Scope Resolution Operator "::".
Like any other PHP static variable, shared properties may only be initialized using a literal or constant; expressions are not allowed.
So while you may initialize a shared property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

Shared memory limit in php.ini:
shared_memory_limit = 16M

This sets the maximum amount of shared memory in bytes that ALL scripts are allowed to allocate.
To have no shared memory limit, set this directive to -1.
To have an unlimited shared memory limit, set this directive to 0.
When an integer is used, the value is measured in bytes. Shorthand notation may also be used.

Shared variables and properties are initialized only in first call of a function/class and every time the shared memory is empty.

Scenarios: Caching, performance optimization, counters, progress indicators, parallel queues, etc.

Test script:
---------------
Foo.php: static class property
<?
class Foo
{
    shared $cache_shared = "hello"; // public static shared

    private shared $_cache_shared = "hello"; // private static shared

    protected shared $_cache_shared = "hello"; // protected static shared

    shared static $cache_static_shared = "hello"; // invalid, shared implies static

    $var1 = "hello"; // public non-static non-shared
}


Foo2.php: static variable
<?
function some_big_calc()
{
    shared $a = array();
}


Scenarios:
some_class::$shared_cache = array(...);
some_class::$shared_counter++;
some_class::$shared_progress = 0.3;


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-04 20:29 UTC] thbley at gmail dot com
-PHP Version: 5.4.0 +PHP Version: 5.5.0
 [2012-03-04 20:29 UTC] thbley at gmail dot com
update version :-)
 [2012-03-05 00:19 UTC] johannes@php.net
It is a design decision in PHP not to have such global things in PHP itself. Addon modules (you mentioned apc) can add utilities for this, though.

Some reasons include: 
- PHP can be embedded in any application which might add constrains
- correct locking can be hard
- such a feature makes scaling over multiple servers hard
- it messes with copy-on-write
- ...
 [2012-03-05 00:19 UTC] johannes@php.net
-Status: Open +Status: Not a bug
 [2012-03-05 02:26 UTC] thbley at gmail dot com
> It is a design decision in PHP not to have such global things in PHP itself.

All PHP frameworks are offering some kind of caching with shared disk or shared memory. And I haven't seen a bigger project not using this stuff. It's time to rethink this decision.

> - correct locking can be hard

I think the way APC does this it, should do it.

> - such a feature makes scaling over multiple servers hard

It would be enough to have it on one server. For those who want more, can still send the data to memcache.

Here is a new example: https://gist.github.com/1975954
 [2012-03-05 03:55 UTC] rasmus@php.net
And APC is still not stable and portable enough to include in PHP, so saying to do 
it like APC doesn't mean much. Doing it like APC means not putting it not the 
core. And if you want something just like APC, just use APC. Most of the 
frameworks already do and they include a slower fallback for servers without APC.
This is not going to be baked into the object syntax directly since we have all 
the magic methods you need to hook it in yourself.
 [2012-03-05 05:44 UTC] thbley at gmail dot com
I think it shouldn't be a big issue to get a small portion of the apc or memcache code in the core and make it stable. In the end of 2005, there was the decision to include apc in the core. So I think it is not a bad idea to look at this again.
 [2012-03-05 05:53 UTC] rasmus@php.net
APC will eventually go in, but it is not going to be directly mapped to any sort 
of special shared keywords. Like I said, there are plenty of ways to map this in 
userspace via the various magic methods.
 [2012-03-05 10:50 UTC] thbley at gmail dot com
Magic methods are nice but checking if the extension is available and enabled, maybe compile it before, implement the whole interface, define some good error handling, include the library, implement the fallback for non-apc, ... a lot of work than could be handled by a one-liner.
People love sth. like session_start(); $_SESSION["hello"] = "world"; without knowing all the stuff behind it.
 [2012-03-05 15:38 UTC] rasmus@php.net
Sure, and that one-liner could be:

session_set_save_handler(new MySessionHandler);

where all that work is done by a userspace implementation.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 26 05:00:02 2025 UTC