Skip to content

Extensions to PHP5 core #8

@thekid

Description

@thekid

Scope of Change

The PHP5 core would be changed in numerous ways. This would include new
syntax, less restrictions and more features, realized via patches to the
Engine and other sourcecode.

Rationale

PHP5 has decent OO support but is somewhat restricive in some areas and
misses some nice features. The PHP development team has strong feelings
on some of the suggested changes (partially due to BC reasons, partially
due to personal views on what is "right" and what is "wrong", partially
because the "PHP is not Java / C# / Perl / Delphi / ..." syndrome or
"because this makes PHP too complex"), thus preventing them from being
realized.

Functionality

The following changes would be incorporated into the new Engine:

a) Syntax changes and additions

  • A keyword "with"
  • An optional throws clause to methods
  • A catch-all-exceptions mechanism
  • Packages support
  • A package access level
  • Enumerations
  • Annotations support
  • Finally
  • Keywords would be allowed as class and method names where possible
  • Automated getters and setters
  • A keyword "synchronized"
  • Return type hints (unchecked, may be retrieved via Reflection)
  • Class instance creation expressions

b) New features

  • Operator overloading
  • "self" reflects runtime class

c) Removed restrictions

  • No implementation check during inheritance
  • Userland exceptions would no longer be forced to extend from the
    built-in exceptions
  • An interface would be added to identify an exception. Userland
    classes would have to implement it

d) Removed cruft

  • Magic quotes would completely disappear
  • register_globals would be nuked
  • Compat mode with Zend Engine 1

e) Changes in errorhandling

  • NULL->method() would result in a NullPointer instead of Fatal errors
  • Passing an incorrect argument to a type-hinted parameter would result
    in an IllegalArgument

f) Configuration

  • Safe mode (and alike) would be replaced by a permission manager

Details

  • The keyword "with":
    with introduces a block of code which is auto-populated by a variable
    named "self".

    with ($tree->addNode(new Node('child'))) {
    $self->setAttribute('id', 10);
    $self->setAttribute('name', $name);
    }

  • An optional throws clause to methods:
    As in Java, one may add a throws clause to methods that indicates
    which exceptions are thrown from it.

    public function connect() throws IOException, ConnectException {
    // ...
    }

    This clause is, of course, optional and no checks are inferred from
    it (e.g., the caller of this method is not required to catch these
    exceptions). The reflection API provides a way to retrieve these
    exceptions.

    For details, see
    http://experiments.xp-framework.net/?arena,php5,exceptions

  • A catch-all-exceptions mechanism:
    Catch-all is realized by ommitting the exception type from the
    catch clause.

    try {
    // ...
    } catch ($e) {
    // ...
    }

    This is identical to writing "catch (IException $e)" (see also:
    add an interface instead of built-in exception).

  • Packages support
    A new keyword "package" begins a block of code that puts every
    contained class, interface or enumeration (see below) into it.

    package lang {
    class Object { }
    class String extends Object { }
    }

    The package-class-separator is the tilde character (). Thus, the
    String class can be addressed by its fully qualified name,
    "lang
    String" (the fully qualified name is the only way to
    address packaged elements in a generic way).

    An additional function called "import" creates short aliases for
    these long names.

    import langString, langObject;
    import deschlundwebservices~ServiceLocator as Locator;

    $class= 'debinford6100Power';
    $alias= 'MorePower';
    import $class;
    import $class as $alias;

    Name clashes cause a Fatal (Compile-Time) Error.

    For details, see
    http://experiments.xp-framework.net/?arena,php5,packages

  • A package access level:
    The package keyword may also be prefixed to a method to declare its
    access level package-wide (in addition to public, private and
    protected).

    package io {
    class File {
    package function handle() { }
    }
    }

  • Enumerations:
    A new keyword "enum" is introduced.

    enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }

    Enumerations have a static function "values()" that returns the
    values as an array.

    foreach (Suit::values() as $value) { }

    The sizeof() function applied to an enumeration returns the numbers
    of elements in an enumeration (for the above example, 4).

  • Annotations support:
    Annotations (metadata) may be embedded within the sourcecode using
    the @ sign to prefix them.

    class SoapClient {
    [@debug('level=1')] public function setTrace(LogCategory $log) {
    }

    [@remote] public function invoke($params) throws InvokationException {
    }
    

    }

    The Reflection API offers a way to retrieve these annotations.

    For details, see
    http://experiments.xp-framework.net/?arena,php5,syntax

  • Finally:
    A new keyword finally is introduced which is called regardless of
    whether an exceptions is raised or not.

    try {
    // ...
    } catch (IOException $e) {
    // ...
    } finally {
    $file->close();
    }

  • Keywords would be allowed as class and method names where possible
    The keywords class, array, list and so on would be allowed as class
    an method names (where unambiguous):

    class List { }

  • Automated getters and setters:
    A new form of declaring getters and setters for variables would be
    introduced:

    class String {
    public $buffer get $buffer set setBuffer;
    }

    $s= new String();
    $s->buffer= 'Hello'; // calls setBuffer() with 'Hello' as argument
    var_dump($s->buffer); // reads member "buffer"

  • A keyword "synchronized":
    The keyword "synchronized" would protect a block from being executed
    more than once from simultaneously running threads or even instances.

    synchronized {
    // ...
    }

    The synchronized directive takes an optional model as argument (compare
    to "declare(ticks=1)"). Using the model "flock", one could prevent, for
    example, a cron job from running twice at the same time.

    synchronized(model=flock) {
    // ...
    }

    When a synchronized block is hit and another instance tries to execute
    the same code, it would have to wait for the first instance to finish
    before being able to continue.

  • Class instance creation expression
    A class instance creation expression is used to create new objects that
    are instances of classes.

    Example 1:

    abstract class Comparator {
    abstract public function compare($a, $b);
    }

    class ArrayList {
    public function sort(Comparator $c) {
    usort($this->elements, array($c, 'compare'));
    }
    }

    $list->sort(new Comparator() {
    public function compare($a, $b) {
    return strnatcmp($a, $b);
    }
    });

    In this case, the class being instantiated is a subclass of the
    Comparator class.

    Example 2:

    interface Comparator {
    public function compare($a, $b);
    }

    class ArrayList {
    public function sort(Comparator $c) {
    usort($this->elements, array($c, 'compare'));
    }
    }

    $list->sort(new Comparator() {
    public function compare($a, $b) {
    return strnatcmp($a, $b);
    }
    });

    In this case, the class being instantiated is a class implementing
    the Comparator interface.

  • Operator overloading
    For details, see
    http://experiments.xp-framework.net/?arena,php5,operator_overloading

  • "self" reflects runtime class
    This would make the following possible:

    class A {
    public static function getInstance() {
    return new self();
    }
    }

    class B extends A {}

    var_dump(B::getInstance()); // object(B)

    For details, see
    http://experiments.xp-framework.net/?arena,php5,syntax

  • No implementation check during inheritance
    The following would not be checked upon (not even in strict mode):

    class Window {
    function show(Point $point) { }
    }

    class MyWindow extends Window {
    function show() {
    parent::show(new Point(100, 200));
    }
    }

    Currently, this would give an E_STRICT warning

  • Userland exceptions would no longer be forced to extend from the
    built-in exception. An interface would be added to identify an exception.
    Userland classes would have to implement it.

    For details, see
    http://experiments.xp-framework.net/?arena,php5,exceptions

  • Magic quotes would completely disappear
    Magic quotes are a big nuisance and don't protect you from
    anything. The magic quotes on request variables as well as the
    magic quotes that are applied at runtime would no longer exist.

  • register_globals would be nuked
    The only way to access HTTP request data is via the $_*-
    superglobals or via the php://input stream.

  • Compat mode with Zend Engine 1
    The compatibility mode is kept for backwards compatibility und
    would be removed. It's not like this new engine would be compatible
    with PHP4 anyway.

  • NULL->method() would result in a NullPointer instead of Fatal errors
    Calling a member function of a non-object can be safely caught and would
    therefore cause a php~NullPointerException to be raised instead of a
    fatal error

  • Passing an incorrect argument to a type-hinted parameter would result
    in an IllegalArgument

    function add(int $a, int $b) { }

    add(1, '2'); // php~IllegalArgumentException is thrown.

  • Safe mode (and alike) would be replaced by a permission manager
    The permission manager would offer a much more open API for systems
    administrators to control exactly what a user is able to do and
    what not.

Dependencies

PHP5

Related documents

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions