Skip to content
exos edited this page Feb 27, 2012 · 4 revisions

From JavaScript

Some examples, from JavaScript to Proto applicance:

Scope emulation

In JavaScript:

var MyModule = {

    property: 'some property',

    getProperty: function () {
         return this.property;
    },

    submodule: {
         otherProperty: 'other property',
    }

};

PHP-Proto:

<?php

$MyModule = new Scope(array(

    'property' => 'some property',

    'getProperty' => function (\Proto\SelfArgument $self) {
           return $seld->property;
     },

    'submodule' => new Scope(array(
           'otherProperty' => 'Other property'
     ))

));

Classes

Javascript:

// Constructor or Initialize
var MyClass = function (options) {
     this.property = options.property
}

MyClass.static = 'this is static';

MyClass.staticMethod = function () {
     // this is static
}

MyClass.prototype.member = 'standar member';

MyClass.prototype.getMember = function () {
     return this.member;
}

var $instace = new MyClass(options);

In PHP with Proto:

<?php

// Constructor or Initialize
$MyClass = Proto\Objetc::create(function (\Proto\SelfArgument $self, array $options) {
       $self.property = new Proto\ScopeVar($options['property']);
});

$MyClass->static = 'this is static';

$MyClass->staticMethod = function (\Proto\SelfArgument $self) {
       // This is static
};

$MyClass->prototype->member = 'standar member';

$MyClass->prototype->getMember = function (\Proto\SelfArgument $self) {
     return $self->member;
}

$instance = $MyClass($options);

Singleton Design Pattern

This is only an example of one design pattern, for others patterns view the wiki page

JavaScript:

var Singleton = function () {};

Singleton.instance = null;

Singleton.getInstance() {
     if (!Singleton.instance)
          Singleton.instance = new Singleton();

     return Singleton.instance;
}

Singleton.prototype = {
      property: 'my property'
};

var a = Singleton.getInstance();

PHP with Proto:

<?php

$Singleton = Proto\Object::create(function () {});

$Singleton->instance = null;

$Singleton->getInstance = function ($Scope) {
      if (!$Scope->instance)
            $Scope->instance = $Singleton();

      return $Scope->instance;
};

$Singleton->prototype = array(
      'property' => 'my property'
);

$a = $Singleton->getInstance();
Clone this wiki locally