Sep 08

Read-Only Member Variables with PHP 5

I was talking about Object Oriented Design with someone the other day and the topic of making an object’s member variable read only came up. This is normally a simple task, simply define the member variable as protected or private within the class definition. That essentially makes it inaccessible to any code outside of the class it is a member of. One would then typically define an access function that allows outside code to read the variable. Here is an example:


class readOnly
{
    private $readOnlyVar;

    public function __construct()
    {
        $this->readOnlyVar  = 'This is read only';
    }

    public function getReadOnlyVar()
    {
        return $this->readOnlyVar;
    }
}

$readOnly = new readOnly();
echo $readOnly->getReadOnlyVar();

$readOnly->readOnlyVar = 'Trying to change this';

Running that code would produce the following output:

This is read only
Fatal error: Cannot access private property readOnly::$readOnlyVar

Read the rest of this entry »

Jul 27

Today is System Administrator Appreciation Day!

Today is National System Administrator Appreciation Day. Everyone who has a Systems Administrator or just someone who handles most of your IT should be recognized. Take your local man/woman out for a drink and show them how much you appreciate them! I know Cory and I will be milking it today.

Jul 26

BarCampOrlando Video

BarCampOrlando is coming in a couple of months. Myself and some of the guys from NFi will be attending. The BarCamp guys have released an informational video and button to promote the event.

Get the code to post yourself and help spread the word:

May 31

Wordpress Permalinks do not work with PHP 5.2.1 and IIS

We recently upgraded our servers at NFi from PHP 5.1.2 to PHP 5.2.2. We needed to upgrade to fix a bug in PHP’s COM object support. PHP 5.2.2 and PHP 5.2.1 have proven to be buggy on windows and broken things that have previously worked. Upgrading to 5.2.2 introduces a bug in the mysql module that keeps outputting the following at the end of every script:

Error in my_thread_global_end(): 3 threads didn’t exit

Wordpress is based upon MySQL so this obviously won’t work even though the error is present even when non MySQL code is being run. Downgrading to PHP 5.2.1 fixes that problem. You are not in the clear yet though. With PHP 5.2.1 there is a problem with the standard method Wordpress uses for permalinks.


The default method for using permalinks with Wordpress is to use the following URL style:

/index.php/2007/05/31/sample-post/

This will not work with PHP 5.2.1 though. It appears that it treats that location as the full path to the file to serve. It looks for the folder index.php instead of running the actual script. Since the folder doesn’t exist you get a 404 File Not Found Error.

Moving down yet again to PHP 5.2.0 will fix the problem and that is what I am now running with this blog. Alternatively you can use ISAPI Rewrite for WordPress which will rewrite your URL’s to not use the standard style. There is another popular Wordpress Permalink ISAPI Filter though that still relies upon the /index.php/%year%/%monthnum%/%day%/%postname%/ style. If you are using this filter you will need to use PHP 5.2.0.

Apr 26

MySQL Planning to Go Public

MySQL has reached the $50 million revenue mark and is gearing up towards an IPO. This could prove to be an interesting time for MySQL and the open source database world for web 2.0. I am interested in what lay ahead, how MySQL could be affected and whether it may be a good investment opportunity ;).

Read more from my post at the NFi Studios Blog.

Apr 19

PHP For vs. Foreach

I have always believed that iterating an array using a foreach loop in php was slower than iterating through the same array using a standard for loop. I figured this because there is no foreach equivalent in the underlying native code that php is run on and therefore a standard for loop didn’t require any additional translations.

I have never read any concrete evidence as to which is faster and a quick google search didn’t provide any results. Thus I decided to setup a benchmark test myself. The question in mind for me was “Is it more efficient to loop through a given array with a for or foreach loop?” This is also assuming a standard indexed array and not an associative array or hash table. since you would pretty much only use a foreach for that anyway.

The test involved first filling an array 100,000 items by just counting up from zero. A for loop counting to 100,000 was then setup along with a foreach loop set to iterate through the pre-filled array. An extra loop was setup around each of the test loops to do the iterations multiple times. I like to do this to get more of an average running time because using time as a measurement in a multitasking environment isn’t exactly 100% accurate. The body of each loop was merely a continue statement to move on to the next cycle.
Here is the php code:


set_time_limit(0);
$array = array();
echo "Filling arrayn";

for($index=0; $index < 100000; $index++)
{
     $array[$index] = $index;
} 

$start = time();
for($iteration = 0;$iteration < 10; $iteration++)
{
     for($index=0;$index < 100000;$index++)
     {
          continue;
     }
}
$end = time();

$duration = $end - $start;
echo "For loop: ".$duration;$start = time();

for($iteration=0; $iteration < 10; $iteration++)
{
     foreach ($array as $value)
     {
           continue;
     }
}
$end = time();

$duration = $end - $start; echo "nForeach loop: ".$duration; 

The results went against what I originally thought about php. I changed the iteration counts and the array size to make sure there wasn’t any affect due to loop setups or such. The results were nearly always the same. The for loop took twice as long to iterate than the foreach. Performance with any code is highly dependent on the context it is used in (especially in php) but this shows me my original thoughts were wrong.

Results for the above:

Filling array
For loop: 21
Foreach loop: 11

The benchmark was run on a Macbook Pro 1.8 Ghz Core Duo with 1.5GB ram. The test was run via CLI on php version 5.2.0. No optimizers or opcode cache was used. Try it out on different configurations and post your results in the comments.

Mar 21

Stuck Macbook Pro DVD

I have been a long time Windows user but recently purchased a Macbook Pro since they can now run Windows anyway. After only 2 days, Macintosh didn’t get off to a great start. I watched a DVD on the notebook and upon finishing, the dvd wouldn’t eject. Everytime I pressed the eject button you could hear the drive start to eject the disk and it would sound like it bogged down then you could hear the drive suck the dvd right back in. The dvd never popped out at all. The dvd would load up and play everytime but I couldn’t get it out of the macbook. I even tried restarting.

I resorted to searching the web for people with similar problems. I found many but they were mostly situations where the macbook didn’t recognize the dvd and therefore didn’t give the eject option to begin with and then cause booting problems. They all suggested to use cardboard to stop the disc from spinning during bootup and it would then eject. Even though my problem was similar I decided to give it a try. No luck. I tried lodging the cardboard in ways to help guide the disc out but no luck either.

You could hear the disc slam into something as it tried to eject. I resorted to just tilting the computer in different ways while trying to eject the disc. After many many tries it finally just came out! I had the macbook tilted forward about 45 degrees with the cd drive facing slightly downward. I wanted to see if this problem would persist and if I would need to take it in so I kept inserting an ejecting any cd/dvd I could find multiple times. NONE of them jammed.

dvdWhat was the cause of my dvd jamming then? I looked closely at the dvd and could see that it was ever so slightly warped. This was no doubt due to the case the dvd came in. It was one of those that you have to really pull on the dvd to get it free from its holster, thus bending it. The cd/dvd slot on the macbook is so slim it must have been hitting something on its way out.

This seems easily avoidable by Mac. Can’t they implement some sort of guide system in the cd/dvd drive to help guide the disc out and prevent this problem? After all, the dvd cases like mine are pretty common and the dvd was barely warped (like a mm maybe).

-

Mar 01

I am a DLSB!

Below are my results from my programmer personality test.

Your programmer personality type is:

DLSB

You’re a Doer.
You are very quick at getting tasks done. You believe the outcome is the most important part of a task and the faster you can reach that outcome the better. After all, time is money.

You like coding at a Low level.
You’re from the old school of programming and believe that you should have an intimate relationship with the computer. You don’t mind juggling registers around and spending hours getting a 5% performance increase in an algorithm.

You work best in a Solo situation.
The best way to program is by yourself. There’s no communication problems, you know every part of the code allowing you to write the best programs possible.

You are a liBeral programmer.
Programming is a complex task and you should use white space and comments as freely as possible to help simplify the task. We’re not writing on paper anymore so we can take up as much room as we need.

Nov 16

Evolution of a Programmer

Read Here. This is pretty funny yet true. It won’t make much sense to any non professional programming geeks. I’d like to see the evolution in other languages like php,java, etc. The bit about managers is f’in hilarious because it is so true. Reminds me how nice it is to not be working in a big corporate environment.

Oct 09

Social Networks Can be Niche

“Not all online social networks are the same, according to new research released this week. Internet research firm, comScore Networks, said on Thursday that significant age differences exist between the user bases of these websites.”

“While the top social networking sites are typically viewed as directly competing with one another, our analysis demonstrates that each site occupies a slightly different niche,” commented Jack Flanagan, executive vice president of comScore Media Metrix.”

This is very true in my opinion. That is why we believe that Racing Kids and MyBabyPortal will be successfull. There is no reason why people wouldn’t want to create accounts in multiple social networks. Social networks are based on connecting with other who share your interests. What better way to connect than through a more specific network than something like Myspace? Instead of competing with Myspace focus on something that is more targeted and just give your users ways to market themselves more. That is what I feel most people like social networks for, to market and display what they are doing with their lives.