ruby ported to php
This project is maintained by shuber
A port of ruby 2.0 to native php 5.4+
include
, extend
, prepend
and their associated callbacksObject
and Kernel
methods including super
, send
, respond_to?
, method_missing
Module
methods including alias_method
, define_method
, and attr
accessors?
using this syntax $this->{'empty?'}
$this->{'@name'}
$this->{'@@name'}
$this->{'$redis'}
BasicObject
, Kernel
, Object
, Module
, Method
, UnboundMethod
Array
, Hash
, String
, Enumerable
, Comparable
Phuby
functionunderscore
naming conventionsIf you're using Composer
simply require the shuber-phuby
package.
Otherwise you can download the phuby
tar/zip or git clone
this
repository somewhere in your php include path.
Then inside of your php files you can require 'phuby/phuby.php'
to load the library.
You can integrate Phuby
with your code in 3 ways:
class Blog extends Phuby\Object { }
This is useful when your class needs to inherit from an existing library and
can't extend Phuby\Object
.
class Blog extends ActiveRecord\Base {
use Phuby;
}
Phuby
functionThis allows you to inject Phuby
features into any object.
echo Phuby('this is a sentence.')->upcase;
Phuby([1,2,3])->each(function($number) {
echo $number;
});
echo Phuby(7)->days->from_now;
string
becomes Phuby\String
string
becomes Phuby\Module
if it is a class namearray
becomes Phuby\Hash
array
becomes Phuby\Array
if it has all numeric keysnumber
becomes some type of Phuby\Numeric
(float or int)object
becomes a Phuby\Proxy
which allows us to integrate Phuby
into specific object instancesPhuby()
namespace resolutionPhuby()
aliases e.g. Array => ArrObject
use Phuby
instead of inheriting Phuby\Object
__callStatic
delegates to Module
instancesArgumentError
Exception
NameError
NoMethodError
RuntimeError
StandardError
coerce
const_missing
marshal_dump
marshal_load
method_added
method_removed
method_undefined
singleton_method_added
singleton_method_removed
singleton_method_undefined
to_xxx
append_features
extend_object
extended
included
inherited
initialize_copy
initialized
method_missing
prepended
prepend_features
Usually I try to structure methods so that they have as few return
statements or endpoints as possible (1 ideally). In this project I'm using guard style conditions at the beginning of methods and I'm starting to see the beauty of how readable and simple the source code reads. I'm using many if
and return
statements in favor of else
and if else
. if
expressions are never written inline and the statements are separated by newlines. Brackets { }
are only added to if
and foreach
loops if necessary. It makes source code files longer but everything is condensed horizontally and naturally less than 80 characters most of the time. It makes me appreciate the enforced whitespace in python more.