Phing is a PHP build tool that you might be familiar with or you might be already using. The official Phing website can be found at http://www.phing.info/ and the initials stand for PHing Is Not GNU make. You can download or install it as a PEAR package and as long as you are a bit familiar with XML it is really easy to learn how to use. So, if you are reading past this point it means that you have actually used Phing before and that you are familiar with its CopyTask for copying individual files or filesets from one location to another.
Recently I was trying a new build file with Phing and came accross an interesting issue. The result of building a target that included copying a previously defined fileset from one location to another had a somewhat unexpected result. An empty folder that was contained in the source location for which all users had read, write and execute permissions on, was copied accross but the permissions were not kept. I was not receiving any errors and the folder was actually created in the destination folder. As it turns out the version of Phing that I was using did not support copying an empty folder with its permissions to a new location. So if you are having a similar issue do upgrade your package to the 2.5.0 version of Phing (as of the time of writting this is the latest stable release of Phing on PEAR). You can perform the upgrade quite easily as you would with any other PEAR package.
In PHP the Scope Resolution Operator (which is actually the double colon or Paamayim Nekudotayim as it is its official name) has many uses. From the PHP manual pages (Manual) I quote
The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class. When referencing these items from outside the class definition, use the name of the class.
So, I recently decided to go ahead and test this operator in a bit more detail and discovered something I did not know about the way PHP decides to give you access to its public methods.
Continue reading “PHP: The Scope Resolution Operator (::)”
Let us consider the following php class
This post is about adding captcha verification in the comments section of the Yii demo blog. This post was triggered from two comments posted on the page of the tutorial (Comment 1 and Comment 2). In order for you to follow through the current post you need to have the Yii framework in place and you need to have followed all the steps of the Blog Tutorial up untill the “Creating and Displaying Comments” section. So, let’s start:
Continue reading “How to add Captcha verification in the comments of the Yii blog demo”
If you have followed the tutorial you should by now have a working Leave a comment form in place that is displayed underneath each individual post of your blog web application. The form should contain the Name, Email, Website and Comment fields that will populate the corresponding properties of your Comment model. The Post ID should be set on submit, the Status of the Comment should also be set on submit and the Time Created should be set just before saving the Comment (if this is a newly created comment).
PHP lets you specify the Error Reporting level, so that when a piece of your code is run based on this setting of the php.ini file the generated errors and/or warnings will be shown or not. Generally speaking in a development environment it is recommended to set this as high as possible so that you are informed about all the little details of your code whereas in a production environment this setting is usually set to something less “explanatory”.
In order to set the value of the error_reporting directive php constant values and bitwise operators are used, as they are described in the PHP manual . The setting can be set during runtime using the php error_reporting() function (manual) passing as a parameter the int value calculated based on the constant values and bitwise operators used. The function returns the integer value of the previous error_reporting level.
Now if you wish to find out what is your current error_reporting level you could go and have a look in your php.ini file or you could use the error_reporting() function with no parameters, in which case the function will return an integer value representing the current error reporting level. If you go via the php.ini file you will see the names of the constants used and the operations applied to produce your current reporting level but if you try to get a similar result via a php file that you have written you might notice that it is more difficult to understand what is going on since now you are only dealing with an integer value and not with the named constants. So which is the description of your error reporting level and how can you retrieve it from within a php file?
Continue reading “How to get your Error reporting constant values”
Noticed today that there are more than one ways to access the constants of a class when using the class name, or when using an object of the class to access the constant or even when using an object of the class as the value of a property of another class. The different ways are shown in the code snippets that follow along with a parse error that is generated when the constant is accessed as part of an object’s property.
Continue reading “PHP class constant values”
In PHP there are two special keywords self and static that play an important role in classes that inherit static methods or member variables. They resemble the use of $this for instanciated objects. $this is used to access member variables and methods of an object of a particular type. self and static can be used in a similar manner for accessing static methods and variables from within a class that defines or inherits them. So, let’s see an example of the use of self and static and of the major difference between them. Let us assume that you have two classes one called base and one called derived. Base defines two static variables called name with a value of ‘base’ and number with a value of 0. Derived extends base and defines the same two static variables but now name has a value of ‘derived’ and number has a value of 1.
Continue reading “PHP self vs static”
Recently I was trying to test a small piece of PHP code and run into the magic constant __CLASS__ . I quote from the PHP manual (1)
The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. The class name includes the namespace it was declared in (e.g. FooBar). Note that as of PHP 5.4 __CLASS__ works also in traits. When used in a trait method, __CLASS__ is the name of the class the trait is used in.
So I thought that when I use __CLASS__ it will return me the class of the object that executes the code that includes the echo of __CLASS__. But as it turns out this is not true if __CLASS__ was echoed from within a method defined in a base class and inherited from a child class. As it turns out in that case __CLASS__ returns the name of the class that contains the defined method and not the name of the class that inherits the method and executes it. Consider the following example:
Continue reading “PHP __CLASS__ and get_class”
Here is a funny story about an “error” I thought I was dealing with recently with PHP and arrays. Assume you have a simple array (elements are keyed by incremental numbers – default assignment when you use $a = 1) with many elements (let’s say 10.000) whose unique values you wish to save in your database for later use. So how did I choose to implement this? I run my array through PHP’s array_unique and then serialize it and then store it in the database for later!
Continue reading “Array_unique funny story”