PHP: The Scope Resolution Operator (::)

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.
Let us consider the following php class

Continue reading “PHP: The Scope Resolution Operator (::)”

How to get your Error reporting constant values

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”

PHP class 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”

PHP self vs static

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”

PHP __CLASS__ and get_class

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”

Array_unique funny story

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”