JavaScript Objects Coding Style

JavaScript vs. PHP

Continuing from my post about PHP arrays coding style and following the comments of that post, I’d like to write a bit about JavaScript objects’ coding style.

You perhaps know that the term object is quite undefined or under estimated in the JavaScript world, but I’d speak about the key/value pairs in JS commonly formatted like that:

var obj = { key : 'value' }

Here you can add more and more key/value pairs, but what’s different from the PHP associative arrays and what’s the same and should be cosidered.

The Same as PHP?

I wrote about the alignment in PHP and hashes. Than I showed how I align them:

$arr = array(
   'short'   => 'val',
   'longkey' => 'val'
);

In JavaScript you should use the same technique of alignment:

var obj = {
   'short'   : 'val',
   'longkey' : 'val'
};

Some Differences

Yes, there are more differences, which is normal. First of all you don’t have the => notation in JavaScript and a : is used. Second and most important you cannot add a trailing comma after the last key/value pair. Note that in PHP that’s fine!

// that will throw an error in MSIE
var obj = {
   'short'   : 'val',
   'longkey' : 'val',
};

while this is OK in PHP and it’s encouraged:

$arr = array(
   'short'   => 'val',
   'longkey' => 'val',
);
Posted in javascript, micro tutorial, PHP, web development | Tagged , , , , , , , , , , , , | 1 Comment

The Difference Between .live() and .delegate and Which One Should be Used

There are so many posts about that topic out there, but I’d like to be rather breve! The delegate() method comes with the last major version of the jQuery – 1.4 and it gives a context to the event. So please use delegate() instead of live(), just because not everything’s attached to the document element, but to the selected object.

Posted in javascript, web development | Tagged , , , , , | Leave a comment

PHP Functions: realpath()

Watch Out – Hard Code

Perhaps every developer knows that hard coded paths are no good! The code’s good to be flexible and extensible, but what the way to achieve that? In a typically developed application you’re completely sure the way the folders are nested will be permanent and no change will occur, and that’s maybe true, but however don’t be completely sure.

An Example

Let’s take a typical example. You’ve to access a uploaded file with PHP. By default the files are uploaded in the /tmp folder with PHP given name. That’s why immediately after the upload (the form submit) you’ve to process the _POST and the _FILES arrays and perhaps move the uploaded file somewhere else.

However you’d like to access this file wherever the application is – on the production servers or on the development server or even on the localhost!

I ran into that kind of problem/task. The thing I’d like to achieve was a bit different. I constructed the path with the dirname() function, but there were still ‘/../../’ chunks in it. So the solution is the realpath() function that removes those chunks and converts the path into one “calculated” real path to the file.

Let me show you an example:

// get the uploaded file path
$scriptPath = dirname(__FILE__);
 
// get the realpath to avoid the /../ part of the path
// with dirname they remain in the path
$uploadFolderPath = realpath($scriptPath . '/../../folder_name/');

If you were using dirname() you’d get something like that:

/folder1/folder2/folder3/../../folder2/file.txt

Now with realpath() the result is:

/folder1/folder2/file.txt

That’s more clear and even both are working correctly I’d prefer the second one!

Posted in micro tutorial, PHP | Tagged , , , , , , , , | Leave a comment

Check YouTube Video Existence with Zend_Gdata_YouTube

The Video Has Been Removed?!

Have you ever seen the famous message on YouTube: “This video has been removed by the user.”, but what if you’re trying to grab that video via the Zend_Gdata_YouTube?

That can happen when you’re reading the feed for a channel or a user’s list of videos. In the feed everything’s OK – once the video is uploaded it appears into the feed. After that the user removes the video and visiting the link from the feed you’ll get the message above.

Actually what happens when you try to read this “video entry” within a Zend Framework’s application. There must be some way to catch this message?

Trying to Catch

The answer is pretty simple! Zend_Gdata_YouTube’s throwing an exception and it can be easily cached. Here’s some source code:

$apiKey = 'your_api_key';
$client = new Zend_Http_Client();
$gdata = new Zend_Gdata_YouTube($client, 'my-app', null, $apiKey);
try {
       $gdata->getVideoEntry($video_id);
} catch(Zend_Gdata_App_Exception $e) {
       echo $e->getMessage();
}
Posted in micro tutorial, PHP, zend framework | Tagged , , , , , , , , , , , , , , , | Leave a comment

PHP Associative Arrays Coding Style

No PHP programmer writes code without arrays. Sound strange, but as many programmers there are, as many ways to format the array notation there are.

My advice is to rely on a defined standard. I personally use the Zend Framework’s coding style.

So how to format the code? The first way is on the same line:

1
2
$myArray = array('key1' => 'value1',
		 'key2' => 'value2');

And the other, perhaps more clear way is to place the associative pairs on a new line:

1
2
3
4
$myArray = array(
	'key1' => 'value1',
	'key2' => 'value2',
);

Note that in the second example there’s a comma after the second pair. This is correct PHP syntax and is strongly encouraged!

It’s up to you which way to take. However it mainly depends on the case, but please use standards.

Posted in micro tutorial, PHP | Tagged , , , , , , , , , , , , , , | 5 Comments