I agree that web developers need to get stuff done. But it’s on a developer’s opinion how to get stuff done.

Three days ago Pascal (@zynisch) wrote an article and said that PHP reached its limit. It’s nice comparison of Typo3 Flow vs. Ruby on Rails framework, but I think it’s not very objective. It’s more saying about the frameworks and not about languages.

If you choose a different framework, the comparison can be very different too. And what about than? May I say “Look, PHP is as good as Ruby”?

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


1. CRUD

1.1. Define a property

Rails

field :titlevalidates_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),    )}

1.2. Define a relation

Rails

// in the blog modelhas_many :posts// in the post modelbelongs_to :blog

Yii

// in the blog modelpublic function relations(){    return array(        'posts' => array(self::HAS_MANY, 'Post', 'blogId'),    );}// in the post modelpublic function relations(){    return array(        'blog' => array(self::BELONGS_TO, 'Blog', 'blogId')    );}

1.3.Fetch a record

Rails

Blog.find(my_uid)

Yii

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

1.4. 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'));

1.5. Create a record

Rails

Blog.create(title: 'My Title')

Yii

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

2. View

2.1. Assign something to the view

Rails

@posts = ...

Yii

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

2.2. Templates

Rails (with erb)

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

Yii

// Yii has built in Prado renderrer, but I don't use it, so 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; ?>

2.3. Link to a post

Rails

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

Yii

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

2.4. With i18n

Rails

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

Yii

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

3. Routes

3.1. Static route

Rails

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

Yii

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

3.2. Dynamic route

Rails

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

Yii

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

3.3. Constraints

Rails

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

Yii

'/blog/&lt;blog_id:(index|list)/&lt;action&gt;' =&gt; '/blog/&lt;action&gt;'

4. 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}")    endend

Yii

Doesn’t have built in support. You can try this solution or find another.


Conclusion

Was this comparison about languages? No.

Which one is better? PHP or Rails? I don’t know. You can write mess in both, but it’s very important which tools are used and how.


Note: Originally published at http://sloblog.io/~ujovlado/MIJExiD2mfE/php-hasnt-reached-its-limit-yet, but the platform has been turned off.