PHP: My Missing Manual


When I am reading CRUD source code generated by Gii, I am confused with some PHP syntax and usage. Therefore, I create this post to help.

Disable Short Open Tag

In php.ini, set short_open_tag = on to allow using <? ?> for PHP code.

For those need to mix with XML, please set short_open_tag = off to disable this feature.

With PHP 5.4 or higher, <?= ?> is always available.

Scope Resolution Operator (::)

One of the object operator in PHP.

Double colon (::) is used with class name to access static, constant, and overridden properties or methods of the class from a child class.

For example, in the following code sample, class Yii use public method app() to get the application singleton. Then use -> to access the public property errorHandler which will returned a component defined by CErrorHandler. Last to use -> to access the public property error in CErrorHandler.

Yii::app()->errorHandler->error

Here are some interesting discussions comparing arrow operator and double colon operator:

  1. StackOverflow: Difference between double colon and arrow operators in PHP?
  2. StackOverflow: What’s the difference between :: (double colon) and -> (arrow) in PHP?

Arrow Operator (->)

One of the object operator in PHP.

Arrow operator is used with an instance.

Although I cannot find any page explain it in PHP.net, there are some interesting discussions comparing arrow operator and double colon operator:

  1. StackOverflow: Difference between double colon and arrow operators in PHP?
  2. StackOverflow: What’s the difference between :: (double colon) and -> (arrow) in PHP?

Array Assignment Operator (=>)

Used to assign from right to left to an array on the left.

There is only two sentience to explain in Assignment Operators.

For example, assign string ‘CViewAction’ to the ‘class’ of array page where page belongs to another array as below:

'page'=>array(
    'class'=>'CViewAction',
),

It is very different from C which will use page[‘class’] to describe the ‘class’ of array page.

You will often see foreach with array assignment operator, too.

Reference

  1. PHP.net
  2. PHP: Documentation: Assignment Operators
  3. PHP: Documentation: Description of core php.ini directives: short_open_tag
  4. PHP: Documentation: foreach
  5. PHP: Documentation: Scope Resolution Operator (::)
  6. PHP: PHP Manual: Appendices: php.ini directives
  7. StackOverflow: Difference between double colon and arrow operators in PHP?
  8. StackOverflow: What’s the difference between :: (double colon) and -> (arrow) in PHP?
  9. Wiki: Create, read, update and delete
  10. Yii Framework
  11. Yii Framework: Documentation: CApplication: Public Properties: errorHandler
  12. Yii Framework: Documentation: CErrorHandler
  13. Yii Framework: Documentation: CErrorHandler: Public Properties: error
  14. Yii Framework: Documentation: Yii
  15. Yii Framework: Documentation: Yii: Public Methods: app()
  16. Yii Framework: Tutorials: Automatic Code Generation
  17. Wiki: Singleton variable
  18. Wiki: XML

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s