PHP hasn't reached its limit yet

PHPRuby

Of course, web developers need to get stuff done. However, it's up to them how to get stuff done.

Three days ago a person with nickname Pascal wrote an article and said that "PHP has reached its limit" (web archive). It's a nice comparison of Flow and Rails frameworks, but it's not very objective. It more says about two frameworks, not about languages.

If you choose a different framework, comparison will be much more different too. Should I say "Look, PHP is as good as Ruby" then? :)

I put things together, chose Yii framework and here's the result.

CRUD

Define a property

Rails

field :title
validates_length_of :title, minimum: 1, maximum: 80

Yii

public $title;

public function rules() {
    return array(
        array('title', 'required'),
        array('title', 'length', 'max'=>80, 'min' => 1),
    )
}

Define a relation

Rails

// in the blog model
has_many :posts

// in the post model
belongs_to :blog

Yii

// in the blog model
public function relations()
{
    return array(
        'posts' => array(self::HAS_MANY, 'Post', 'blogId'),
    );
}

// in the post model
public function relations()
{
    return array(
        'blog' => array(self::BELONGS_TO, 'Blog', 'blogId')
    );
}

Fetch a record

Rails

Blog.find(my_uid)

Yii

Blog::model()->findByPk($myUid)

Find something specific

Rails

Post.where(tags: tag, blog: blog).desc(:date)

Yii

Post::model()->findByAttributes(array('tag' => $tag, 'blogId' => $blogId), array('order' => 'date DESC'));

Create a record

Rails

Blog.create(title: 'My Title')

Yii

$blog = new Blog;
$blog->title = 'My Title';
$blog->save();

View

Assign something to the view

Rails

@posts = ...

Yii

$this->render('posts', array(
    'posts' => Post::model()->findAll(), // or whatever
))'

Templates

Rails (with erb):

<% if @post.comments.any? %>
    <%= pluralize(@post.comments.count, 'comment') %>
<% else %>
    No comments
<% end %>

Yii

Yii has built in Prado renderer, but I don't use it. Here's the clean PHP:

<?php if (!empty($posts)): ?>
    <?php echo count($posts->comments); ?>
    <?php echo CChoiceFormat::format('n==1#comment|n>1#comments', count($posts->comments)); ?>
<?php else: ?>
    No comments
<?php endif; ?>

Link to a post

Rails

<%= link_to @post.title, @post %>

Yii

<?php echo CHtml::link($post->title, array('/post/view', 'id' => $post->id)); ?>

With i18n

Rails

<%= my_viewhelper t('mykey') %>

Yii

<?php echo Yii::t('mykey'); ?>

Routes

Static route

Rails

get '/my/demo' => 'Product#list'

Yii

'/my/demo' => '/product/list'

Dynamic route

Rails

get '/products/list/:sort_order.:format' => 'product#list'

Yii

'/products/list/<sort_order:\w+>' => '/product/list'

Constraints

Rails

get '/blog/:blog_id/:action' => 'blog#:action', constraints: { :blog_id => /index|list/ }

Yii

'/blog/<blog_id:(index|list)/<action>' => '/blog/<action>'

Aspect oriented programming

Rails

class Post
    extend ActiveModel::Callbacks
    define_model_callbacks :destroy, only: [:before]

    before_destroy :log

    def log
        Logger.info("Removing post #{self}")
    end
end

Yii

Yii doesn't have built in support. You can try this solution or find another.

Conclusion

Was this comparison about languages? No.

Is better PHP or Rails? I don't know. It is possible to write a mess in both, but it's very important which (and how) tools are used.


This article was originally published at http://sloblog.io/~ujovlado/MIJExiD2mfE/php-hasnt-reached-its-limit-yet, but that platform no longer works. Luckily, it's still available via web archive.