KillerPHP Blog

Advanced PHP


Using a Microservice to extend the lifespan of old apps

March 10, 2016

orm-framework

If you have an old app that works, but it has some problem spots, it might make sense to use microservices to fix these issues.

Sometimes rewriting core modules or classes in an old app, is just not feasible. In these cases, using a microservice to bypass the old code is the way to go. This strategy can work wonders in breathing new life into an app, saving you the cost and headaches of writing a brand new app from scratch.

Bypassing the Apps old ORM with a much quicker ORM

As an example, we recently fixed a huge speed issue (with an old app) that was using an old slow ORM. An ORM is a layer of code, used to interact with a relational database. ORM is short for Object Relational Mapping … just in case you don’t know.

So we used the micro-service pattern (strategy,) to leverage a new faster ORM.The end result: records that took minutes to load, now loaded in 1-2 seconds!

Sometimes you have to break the nerd-rules

We had to duplicate some fields in the database (typically a big no-no,) to make this work. But by using database transactions and database triggers (to keep up the data integrity,) it made sense to break these rules in this situation.

… Besides, these were fields whose value rarely, if ever change.

We deployed Sunday night (as usual) just in case something went wrong on the live server. I am happy to report, all went well.

Stefan Mischook
Killerphp.com

read more


What are Microservices?

February 16, 2016

Hi,

A microservice is basically just a mini app that has these characteristics:

  1. They do only one thing – they are mini apps.
  2. They standalone – they are NOT part of a larger app.
  3. They are language neutral. That means you can write a microservice in Python, and have a PHP Laravel app use it.

I could write a few more paragraphs about microservices or I could just post my recent video on them:

Thanks for watching!

Stefan Mischook
Killerphp.com

read more


Is PHP the redheaded stepchild of Web Development?

January 16, 2015

Hi,

In defense of poor old PHP! The language everyone loves to hate … but so many still uses!

Each language has its’ strengths and weaknesses. So the one you choose is largely a personal thing. That said, the stats don’t lie, most of the dynamic Web runs on PHP.

If any Ruby, Java or Python zealot fundamentalist attack, just mention that Facebook is PHP based. Game, set and match.

🙂

Stef?

read more


Which PHP Framework is best?

October 31, 2014

Hi!

I get asked a lot: which is the best PHP framework? Or, should I choose PHP framework A or should I should PHP framework B?

The short answer: it depends! See below for the deeper answer.

When considering a framework, besides checking out what the uber-nerds are talking about at the time, consider the current cutting-edge thinking about frameworks in general, and be sure your choice of a framework, is up-to-date in that regard.

The no-framework PHP framework

For example, the move these days, is towards the ‘no-framework framework’ style of framework – that’s a tongue-twister! Basically, the more advanced a framework is, the more open it is.

You should be able to drop in components as you need then from any source. So for instance, if you don’t like the ORM layer in a framework you happen to be using, you should be able to easily plugin some other ORM.

At the end of the day, learning one framework or another should not be considered the end of the path – you should expect to have to learn a new framework from time to time … as things mature. The good news is that once you’ve learned your first MVC framework, the rest will come pretty easily since they are all based on the same basic concepts and patterns.

So, choose whichever framework you feel like!

Stefan Mischook
KillerPHP.com

read more


What is Refactoring PHP Code?

January 16, 2014

Hi!

For whatever reasons, I am in the mood to write … and I have a video too! So, what the heck is code refactoring? In a nutshell:

It is the process of code refinement – taking messy code and reorganizing it into much more manageable (cleaner) chunks. Refactoring is such an important part of coding, that I slap my programmers on back of the head, if they fail to refactor their code!

The video:

To summarize:

  • Refactoring is something you should do as you are programming – not 6 months later!
  • A common refactoring strategy is to take big multipurpose methods/functions and create two or more smaller fined-grained functions out of them.
  • Code that could be used in multiple functions, is a good candidate for refactoring.

Thanks!
Stefan Mischook
killerPHP.com

read more


Obamacare Websites Failed because it looks like Spaghetti.

November 17, 2013

spaghetti code

Best way to not build a massive website: load up a canon of nerds and fire them at said website.

🙂

Ok, let’s define that a little better! In a nutshell:

A very large complex software system, has to be designed as a set of smaller independent systems, that communicate with each other using a common set of protocols. These systems/software, should be have a fairly narrow scope in terms of what they do. That means, don’t try to make your software into a ‘swiss army knife’ of functionality.

… This is basic OOP programming but it seems that in real life, many software developers seems to forget this simple principle.

Separating Concerns, or Defining the Logical Parts of Software

The hardest part of architecting software, is figuring out how to break it down into its’ logical components. One common way to do this, is to look at a software from the point of view of the users … for example:

– user
– administrator

So in this case, you might break your software down by ‘user’ functionality and ‘administrator’ functionality. So you would have one team working on the ‘user’ app and another working on an ‘administrator’ app.

Connecting the two Apps together

In case of most PHP apps, you will be tracking information with a MySQL database and so you can use the database as the bridge between the user part of your program and the administrator part. Doing so can really simplify development and it makes it easier to have programmers work independently – without having to talk too much with people working on the other app.

A good Software Architect is Needed!

Of course, when you are developing apps like this, you first need to have an experienced architect who can properly structure the project so that the developers of each part (user and admin) are not stepping on each others toes!

What to take away?

The above example is just an example. For many projects, dividing the app by user type may not make any sense! The key thing you want to take away is that you should look to create your software so that the major functions (and the code associated) is isolated from the code used in other major functions found in your system.

I hope that helps,

Stefan Mischook
killerPHP.com

read more


What are PHP Peekaboo Bugs? What causes them and how to fix them?

September 24, 2013

peekaboo-bugs

Peekaboo bugs are the worst sort of bugs, because they just pop-up randomly and are really hard to track down.

When a PHP peekaboo bug appears, I get very nervous because I know that I might be spending hours trying to track it down. I also get nervous because the peekaboo bug itself, tells me something is rotten with the codebase to begin with.

What causes a Peekaboo bug to appear?

In a nutshell: messy code.

Messy code typically means code riddled with ultra long monster-methods … or as some people refer to them: god methods. If you have a method/function with 200 lines of code and 3 or more nested conditionals – you are begging for a peekaboo to appear!

Squashing the PHP Peekaboo Bug

The first thing to do when peekaboos show up, is to localize (as best you can) where the source of this error is. You may only be able to narrow this down to the class, but that doesn’t matter … what you are looking for is the aforementioned monster-methods or god methods.

Once you’ve identified these, it time to start simplifying/refactoring them and do what Fowler calls ‘extract method’. This is the process of breaking down these massive monstrosities of nerd madness into smaller more manageable chunks.

… So for example, you might take out a few if and if else statements from a method and place them into their own method that the original monster-method can call. Besides making the code much easier to debug and update, you will probably squash your peekaboo bug in the process.

A final tip:

You should always be looking to refactor code AS YOU BUILD your app. Think of it as opportunities when you are coding and you run across a messy chunk of code that can be easily simplified.

Thanks,

Stefan Mischook
killerPHP.com

read more


PHP Data Type Mismatch Crashes CodeIgniter!

September 21, 2013

Learn PHP by Building Web Applications

Hi,

It’s been a long while since I’ve blogged about anything here … been busy building things.

Refactoring a Prototype

Recently, we were refactoring a PHP/CodeIgniter based project, cleaning up the code from a quick prototype we put out. When you have an idea for a piece of software, it is almost always a good tactic to first put out a rough prototype, to see if it has any legs … will anyone find use in your brilliant new idea?

Get user Feedback ASAP!

Another advantage of quick prototyping, is that you can use user feedback to finetune your app to their needs. I became much more successful as a developer doing things this way, over building a rock solid app from the start. Users will always find flaws in your original ideas and many of your ideas will simply not work … so it’s best to get the app into peoples hands as soon as you can.

(more…)

read more


PHP, Zend Framework, Ruby and Web App Frameworks.

May 20, 2012

I recently had a conversation with a good friend of mine who has his finger on the pulse of the web app developer community – whether it be PHP, Ruby and even .Net.

So I asked:

Last we spoke, you mentioned that the Zend Framework was in a do or die situation?

Can you elaborate on that? I am just curious.

Stef

His answer:

Something I’ve noticed is that the idea of a monolithic framework is kind of dying. If you look at ruby on rails, it started off as a monolithic code base maintained by 37 Signals. Because of that, they wrote the Javascript layer, the ActiveRecord implementation for persistence, the view-controller architecture etc…

Rails 3.x is a completely different monster. They replaced their JS layer with jQuery. They replaced their ORM with Arel and ActiveRecord is now a thin wrapper around Arel. They replaced their web server archictecture with “rack” middleware. What this has done is standardized certain portions of the framework to the point where they are truly modular. In the rails community another framework will advertise that it’s “rack compliant” or “built on top of Arel”. What this means is that 37 Signals isn’t *the* ruby company and so they can specialize and delegate, while providing a strong brand that promotes software development best practices.

In the ZF community, ZF 1 worked ok, but it relied on a few anti-patterns (heavy use of singletons in order to ensure that performance wasn’t abismal). ZF 2 is trying to address that, but they have a really small team of strong PHP developers that are working on the core pieces (like the MVC implementation and the persistence). This has made the release of ZF 2 “just around the corner” for about 6 months now.

Also, because the people doing the rewrite wrote the first ZF, they want to clean up all their mistakes and so there’s a bit of ivory tower engineering going on (heavy use of dependency injection and inversion of control patterns), which will make the framework less friendly to beginners. Add to that the fact that Zend_Db was a huge face-palm and that people want integration with Symfony (a competing PHP framework) components, Zend is going to have to give some ground and start making some tough calls to deliver.

Finally, they HAVE TO DELIVER, because ZF 2 is written with the assumption that you’re using PHP 5.3+, which will be a huge shift for large code bases that haven’t seen a serious rewrite since PHP 4. If they don’t make ZF 2 a resounding success, the community around ZF will probably move to greener pastures (my money is on symfony or just not PHP).

my 2 cents,

… Some interesting thoughts, so I continue:

Very cool.

Now that a few years has passed, what do you think of Ruby vs. PHP these days? When we looked at it, I found the libs a little weak .. I imagine all those wholes are filled by now.

Stef

His response:

I think Ruby’s shaping up nicely for getting something done quickly and elegantly. There’s a huge aversion to ivory tower architecture, which I appreciate. I think that there are still performance issues on the whole that are being addressed, but now there are a lot of different ways of dealing with it (or even not dealing with it by using a cloud service like Heroku).

I would also look at erlang. Erlang is a whole different paradigm, but I could see it growing in terms of handling middleware development or places where you need high-volume, high-concurrent systems that are scalable. This is a language that I’m hoping to dive into in 2013, but I think Ruby / Javascript will be my bread and butter.

The Javascript framework wars have really heat up lately, and I would be looking at BackboneJS. It builds on top of jQuery to provide a very nice model for wiring events and writing views / models / controllers in the browser.

My final thoughts

My buddy has been developing web apps for years and has worked with several technologies – he like me, is language agnostic. We pick the best tools/language for the job, and we don’t get too religious about anything in particular.

So does this mean PHP is on its’ last legs or am I advocating Ruby? No, PHP is not going anywhere. I decided to post this just as food for thought … something that you can keep in mind as you develop your skills and your career.

Stefan Mischook
killerphp.com

read more