recked
7/7/2014 - 5:33 PM

OO PHP

OO PHP

##Object-Oriented PHP Based on Object-Oriented PHP for Beginners on Tutsplus

######What is Object-Oriented Programing?

  • It's a style of coding that is used to group similar tasks into classes keeping the code DRY (don't repeat yourself) and easily maintainable. Seems scary because it introduces new syntax and appears more complex than inline code.

######Why DRY?

  • If information changes in your program, usually only one change is needed to update the code. More on DRY here and here (bonus DAMP).

####Objects and Classes

  • What is a class?
    • Classes form the structure of data and actions and use that information to build objects. Think blueprints.
  • What is a object?
    • Objects are built from a class' blueprint. The final product would be the house. More than one object can be built from the same class at the same time, completely different from each other; like a subdivision in the 'burbs.

From here on, examples will be in PHP

#####Structuring Classes

Syntax:

<?php
    
    class MyClass{
        //Properties and methods
      }
?>

After creating the class, declare the object:

$obj = new MyClass;

Use var_dump() to see the contents:

var_dump($obj);