Close

Home » Web Development (Page 2)

‘Web Development’ Category

January 26th, 2012

Code completion in phpStorm with WordPress

phpStorm Logo I use phpStorm pretty much exclusively for PHP development, for over a year.
It simply has some awesome features.

But there are some things that people tend to complain about.
One of the biggest I am going to show you how to fix.

phpStorm by Jetbrains uses a stricter approach for code completion than most PHP IDE’s.
They index all constructs but for everything else phpStorm requires proper phpDoc annotations.

The reason behind this I think is simply for performance to keep it a bit litter and to have faster start-ups.

I will be doing a few of these examples for popular open source projects that have code completion issues do to their standards or simply not having phpDocs at all.

Once you get the hand of this, its pretty easy to do and works quite well.
Some of these issues are seen in all IDE’s not just phpStorm

So here we go, digging into the code.
The comments explain just about everything you need to know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* @see https://gist.github.com/1105126
*/
 
// Also some more hints and fixes
 
// WP functions for adding to the admin bar
// This will provide code completion for admin_bar functions like add_menu()
/** @var $wp_admin_bar WP_Admin_Bar */
global $wp_admin_bar;
 
// Fixing dynamic includes, also so they link properly in the editors environment
require_once(get_template_directory() . '/inc/base.php');
//As is the above require will show an error in phpStorm "Cant resolve target....."
 
//To fix this is easy, this fixes the error and also allows control click on the base.php and opens it
/** @define "get_template_directory()" "/var/www/wp-dev/wp-content/themes/tridium" This is just for the IDE */
require_once(get_template_directory() . '/inc/base.php');
 
// These same methods can be used to fix most errors and completion in phpStorm

Basically for accessing WordPress’s functions and methods that are mostly used eg wpdb, you add this file to the WP root https://gist.github.com/1105126.

I prefer as in the comments of the file to have the class named wpdb and remove the extends.
Like this,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * @property wpdb  $ID
 * @property wpdb  $post_author
 * @property wpdb  $post_date
 * @property wpdb  $post_date_gmt
 * @property wpdb  $post_content
 * @property wpdb  $post_title
 * @property wpdb  $post_excerpt
 * @property wpdb  $post_status
 * @property wpdb  $comment_status
 * @property wpdb  $ping_status
 * @property wpdb  $post_password
 * @property wpdb  $post_name
 * @property wpdb  $to_ping
 * @property wpdb  $pinged
 * @property wpdb  $post_modified
 * @property wpdb  $post_modified_gmt
 * @property wpdb  $post_content_filtered
 * @property wpdb  $post_parent
 * @property wpdb  $guid
 * @property wpdb  $menu_order
 * @property wpdb  $post_type
 * @property wpdb  $post_mime_type
 * @property wpdb  $comment_count
 * @property wpdb  $filter
 * @property wpdb  $term_id
 * @property wpdb  $name
 *
 */
class wpdb {}

Then I drop that file in the WP root.
phpStorm will index the construct and use the listed properties in that file for code completion within the wpdb class.

You should not get duplicate class errors because we are not instantiating it.
Its just there for phpStorm.

I will be doing one of these for phpBB3 and also CodeIgniter so you may want to subscribe and follow future posts.

August 17th, 2011

BenchMark Limbo with ORMs

So the story goes like this….

I have been building a business model and planning out a SaaS application for a future company I am building.

In the process I have been looking at frameworks, ORMs, DBALs, etc… to speed up the build process.

At the same time I have had some concerns.

  1. Memory usage
  2. Process usage
  3. Load time
  4. Learning curve

Learning curve being the smallest issue since all any of the above  (quick build options) are just OOP PHP, but none the less you have to learn their code.

So I have really been struggling with this part. Use quick build options, don’t use them????

So today I decided to do a quick benchmark, nothing special, just load time and memory usage.

I have a MySQL database with 240,000 records in it.

I chose to use Propel ORM because I really like it, but the following benchmark has me in limbo bit…

KEEP in mind, this is not a competition between ORMs or frameworks.

This is just between an ORM which I chose Propel and raw PHP –> MySQL.

Here is the outcome of the benchmarks,

———————————————————————-
ORM results Propel Hydration Demand was used.
———————————————————————-

20,000 Records, APC Opcode Cache on
Load Time: 4.040 seconds. • Memory Usage: 1.03 MiB
Peak Memory Usage: 1.050041 MiB

100,000 Records, APC Opcode Cache on
Load Time: 17.985 seconds. • Memory Usage: 1.03 MiB
Peak Memory Usage: 1.050362 MiB

100,000 Records, APC Opcode Cache off
Load Time: 17.936 seconds. • Memory Usage: 3.32 MiB
Peak Memory Usage: 3.344589 MiB

———————————————————————-
           Raw code
———————————————————————-

20,000 Records, APC Opcode Cache on
Load Time: 0.916 seconds. • Memory Usage: 0.33 MiB
Peak Memory Usage: 0.355274 MiB

100,000 Records, APC Opcode Cache on
Load Time: 2.392 seconds. • Memory Usage: 0.33 MiB
Peak Memory Usage: 0.355274 MiB

100,000 Records, APC Opcode Cache off
Load Time: 2.161 seconds. • Memory Usage: 0.33 MiB
Peak Memory Usage: 0.355274 MiB

So the results are a bit twisted. Was not what I was expecting, higher memory Yes, load time I thought would have been way better.

Also NOTE that this is not an application being benchmarked, it is just one query grabbing records from one table!

While 3.3MG of memory is not a lot to worry about, consider you still need all of your application code which will be more code than the ORM.
18 seconds to collect the data, well, thats just disappointing!

Where do I go from here? Well, I’m not sure.

This application will be huge and have what is planned a large user base.

Saving on resources, load time and scaling is KEY.

I know I can build a simple DBAL that will out perform the above ORM results, so I think that is the direction to take.

What I have found out is that ORM’s and FrameWorks are specifically for rapid development.

If I am going to chew up resources with them on a large scale application, then it is not worth using it.

Using some ideas from certain areas is about as far as I can go for this app, the rest will have to be hand written PHP that will be used in the app.

As a side note, notice the results have APC Opcode Cache on/off, APC Opcode Cache is awesome :)

I think ORMs like Propel and Frameworks like CodeIgniter which I really like both and they have a place in development.

If the application has a scaling limit, then thats where I would use them.

But I don’t think this app is the place.

March 10th, 2011

Top 5 Common Mistakes in Theme Building

I see a ton of theme builders sprouting out of the woodwork everyday.
The problem is a lot of them have great ideas about the design, but the big part (coding) is terrible.

So here are the top 5 mistakes I see in themes, especially WordPress themes..
(more…)