Home > PHP > PHP tricks with object oriented programming

PHP tricks with object oriented programming

September 11th, 2008

Programming PHP the right way as with any other OOP language requires knowledge and understanding of OOP concepts. For those of you who don’t know this by now, OOP stand for Object Oriented Programming. Working on a small project where developer goes trough just a few files is a no problem since your most likely memorize all your properties and methods.

However, working with big project like Magento, or even WordPress can become real pain in a neck if u use only text editor trough your development process. On the other hand, don’t think your off the hook using some fancy IDE studio like NetBeans, NuSphere, or ZendStudio. Problem is that each of them has it’s own problems and glitches resolving object context across multiple project files and so on. Sometimes when you hit $this-> code completion wont return any methods that go with it. So what do you do?

My previous post, Debugging Magento using Krumo, was just a warmup. PHP enables you to use number of built in functions designed to give you necessary information about object context, public files available to that object and so on. If your a beginner or intermediate developer you might wonder why would someone need to know this. I mean no disrespect when calling on intermediate level programmers in same context as beginner level. The thing is, many of the PHP programmers can make extraordinary apps without even a slightest knowledge of OOP concepts. After all, OOP support in PHP is something that was added to design trough-ought the years.

Advanced programming requires you to know OOP concepts and their usage. Otherwise it would not be called advanced, right?

I mentioned before that PHP comes with builtin functions essential for quick retrieval of object information. Here are the functions you can use as your every day help to speed up thing.

  • get_class($object); # returners string
  • get_class_methods($class_name); # returners array
  • get_class_vars($class_name); # returners array
  • get_declared_classes(); # returners array
  • get_declared_interfaces(); # returners array
  • get_defined_constants(); # returners array
  • get_defined_functions(); # returners array
  • get_defined_vars(); # returners array
  • get_included_files(); # returners array
  • get_object_vars($object); # returners array
  • get_parent_class(); or get_parent_class($object); # returners string
  • get_required_files(); # returners array
  • is_a($object, $class_name); # bool
  • is_resource($var); # bool
  • is_object($var); # bool
  • is_subclass_of($object, $class_name); # bool

These are not all get_ or is_ functions available in PHP. I mentioned only those essential as a help to OOP.

One tip on executing all the get_ functions. Most of them you’ll have to echo to see the result in browser, like

<?php echo get_declared_classes(); ?>

Second tip is to var_dump() some of the get_ functions. Given the fact most of them return array as a result, echo would return “array” to browser. So you might do something like this

<?php var_dump(get_declared_classes()); ?>

to get nice output on your browser. Here are some screenshots for you to see.

You can even download sample project I made. All you need to do is to dump it into sour Apache web folder and run idex.php. Try playing around with commented functions.

I strongly recommend reading detailed information on each listed function in official PHP documentation.

Hope this article was helpful for some of you.

Interesting? Share it!
  • Digg
  • del.icio.us
  • Google
  • Technorati
  • Facebook
  • TwitThis
  • description

Related articles

PHP ,

  1. October 24th, 2008 at 10:21 | #1

    Advanced programming requires you to know OOP concepts and their usage. Otherwise it would not be called advanced, right?

    Wrong!
    Advanced does not equal OOP.
    OOP is a methodology and mindset. Advanced is a slush word that just references some vague notion of “beyond the basics”, whatever that is, different for each programmer.

    You can write some really advanced programs without using OOP. Some problems lend themselves to objects, some don’t. Some people don’t realize this, and try to force-fit everything into objects. Especially if the program is a filter or tool, it is better done without objects. Objects are most useful when there is state data and operations unique to the data; the object encapsulates the data and the operations together.

    PHP is very flexible and there’s no “right way” to program in it (except syntax-wise). As you say, it was not designed with objects to start with. Most web applications don’t need objects for solving the problem at hand, but use them to reduce namespace pollution. Anything too complicated object-wise really puts a strain on the server and performance is reduced. All that state data has to be recalculated at the next page view anyway, so why even bother?

  1. No trackbacks yet.