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”Page 5 of 6
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”Alternative to “Notes” for kde
I recently found an alternative to kde’s Desktop sticky notes which was most probably not made for that but none the less it does the trick and it allows you to have control of the location and storage of your notes.
You can use an editor and create a txt file that you will keep your notes in. The good thing is that since this is a file that you have created you can store it wherever you want on your local system and perhaps back it up as you would with every other file. Then you can add a kde desktop widget called File watcher. As soon as you place your widget on your desktop it will only have a single button on it and clicking it will allow you to select the file you wish to “Watch” (check for changes or modifications). Here select your notes file by browsing to your directory.
After completing the steps described above you will have your notes displayed in the File watcher and whenever you update and save the file the widget will be updated automatically. If you wish to edit your file you don’t even have to browse to its location and edit it, you can simply right click on the widget and choose“Run the associated application” which will open your notes for editing. That’s it! Save and you are done…
Counting files in a directory
When browsing your directories command line on your linux box you might want to get a count of the files inside a particular directory. This turns out to be a very easy task and the only things you need to know are the | operator and two basic commands, called ls and wc.
Since you are browsing your directories command line I take it you already know ls (for listing directory contents) but you might not yet know wc!
So first you need to issue an ls -l command which will print on standard output (your terminal) a list of the files in your directory one file at each line. Then we need to pass that result as input to the wc command (which prints information about the lines in a file, or the words in a file etc.). So, we can do that in one line by writing:
ls -l | wc -l
Getting started with MongoDB
This post describes the basics in installing MongoDB in your Debian system, creating a database, a collection with some documents, dropping collections and databases.
If you wish to install mongoDB on your Debian system you should (as root) install the mongodb metapackage (it will install all necessary packages to your system).
Continue reading “Getting started with MongoDB”Relatively positioned elements: How to remove white space
Positioning elements in HTML with CSS creates some problems when rendering the elements to be presented on a website. For example I came across this common problem with relatively positioned elements:
if you relatively position an element an empty space is created in the place where the element was supposed to normally appear
So how can we remove this white gap?
In order to examine this issue let us create an html page with 3 divs. One outer div which includes 2 other divs (div A and div B) and some text that follows the outer div. You can use the following html and css code to reproduce the result presented in the following image. I have chosen to set the background-color of the divs so we can easily identify them.
\Read moreBoxplots using R – Multiple values
(This post explains how to use R to create multiple boxplots in the same graph)
If you have been using R with Rcmdr and have taken a look at the boxplots that you can create with it, then you already know that there are not many options that you can set for the plots. You can only create a boxplot based on one variable from your active dataset (variables with data that you have loaded in your current workspace).
Continue reading “Boxplots using R – Multiple values”PostgreSQL pattern matching indexes
The two basic types of indexes in PostgreSQL are B-trees and Hash. Personally I have only used the B-trees, but recently a friend of mine asked me a question about the way PostgreSQL handles indexes on text fields internally. The question was: “Assume that you have created an index on a varchar field and then you issue a query to retrieve some results. Does your query use the index with the LIKE operator?”
The answer to that actually was No.
Continue reading “PostgreSQL pattern matching indexes”