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:
- StackOverflow: Difference between double colon and arrow operators in PHP?
- 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:
- StackOverflow: Difference between double colon and arrow operators in PHP?
- 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
- PHP.net
- PHP: Documentation: Assignment Operators
- PHP: Documentation: Description of core php.ini directives: short_open_tag
- PHP: Documentation: foreach
- PHP: Documentation: Scope Resolution Operator (::)
- PHP: PHP Manual: Appendices: php.ini directives
- StackOverflow: Difference between double colon and arrow operators in PHP?
- StackOverflow: What’s the difference between :: (double colon) and -> (arrow) in PHP?
- Wiki: Create, read, update and delete
- Yii Framework
- Yii Framework: Documentation: CApplication: Public Properties: errorHandler
- Yii Framework: Documentation: CErrorHandler
- Yii Framework: Documentation: CErrorHandler: Public Properties: error
- Yii Framework: Documentation: Yii
- Yii Framework: Documentation: Yii: Public Methods: app()
- Yii Framework: Tutorials: Automatic Code Generation
- Wiki: Singleton variable
- Wiki: XML