Introduction To Object-Oriented Programming With PHP – Part 2

Before beginning this portion, you may be interested in the following:

  1. How to Setup a LOCAL developement Environment
  2. How to Setup a VIRTUAL Development Environment
  3. Introduction to this course in general
  4. Part 1 of Introduction to Object-Oriented Programing in PHP

Terminology

As I said in part 1, I believe terminology is key to understanding how the particular technology works. With all the resources online, if you understand the terminology, you can use the internet to help you figure out how to do just about anything. With that in mind, here are the new terms for part 2.

Polymorphism

Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface

One good article online is: http://code.tutsplus.com/tutorials/understanding-and-applying-polymorphism-in-php–net-14362

The two main ways polymorphism is used in OOP:

Interface

  • Interface, specifies which methods a class must implement.
  • All methods in interface must be public.
  • Multiple interfaces can be implemented by using comma separation
  • Interface may contain a CONSTANT, but may not be overridden by implementing class

Abstract

An abstract class is a mix between an interface and a class. It can define functionality as well as interface (in the form of abstract methods). Classes extending an abstract class must implement all of the abstract methods defined in the abstract class.

Type Hinting

This is not the same as Type Casting. Type Casting is forcing a variable to be a certain type such as Integer or String.

Namespaces

  • Help create a new layer of code encapsulation
  • Keep properties from colliding between areas of your code
  • Only classes, interfaces, functions and constants are affected
  • Anything that does not have a namespace is considered in the Global namespace (namespace = “”)
  • must be declared first (except ‘declare)
  • Can define multiple in the same file
  • You can define that something be used in the “Global” namespace by enclosing a non-labeled namespace in {} brackets.
  • Use namespaces from within other namespaces, along with aliasing

Files used in class

Full PHP Script used in presentation:

For https://github.com/sketchings/oop-basics

Concusion / Evaluation

If you are interested in taking this as a hands on workshop, I will be presenting this series at different venues, mostly in the Portland area. Most are free, so please drop me an email and I will let you know about upcoming events.

If you HAVE taken the class, please fill out the evaluation so I can continue to improve this and other lessons.

Introduction To Object-Oriented Programming With PHP – Part 1

Before beginning this portion, you may be interested in the following:

  1. How to Setup a LOCAL developement Environment
  2. How to Setup a VIRTUAL Development Environment
  3. Introduction to this course in general

Files used in class

Full PHP Script used in presentation (intro.php): https://github.com/sketchings/oop-basics

If you are interested in learning more, please check out part 2

Concusion / Evaluation

If you are interested in taking this as a hands on workshop, I will be presenting this series at different venues, mostly in the Portland area. Most are free, so please drop me an email and I will let you know about upcoming events.

If you HAVE taken the class, please fill out the evaluation so I can continue to improve this and other lessons.

Introduction To Object-Oriented Programming With PHP

For many programmers, object-oriented programming is a frightening concept, full of complicated syntax and other roadblocks. We’ll be learning the concepts behind object-oriented programming (OOP), a style of coding in which related actions are grouped into classes to aid in creating more-compact, effective code.

Understanding Object-Oriented Programming

Object-oriented programming is a style of coding that allows developers to group similar tasks into classes. This helps keep code following the tenet “don’t repeat yourself” (DRY)and easy-to-maintain.

“Object-oriented programming is a style of coding that allows developers to group similar tasks into classes.”

One of the major benefits of DRY programming is that, if a piece of information changes in your program, usually only one change is required to update the code. One of the biggest nightmares for developers is maintaining code where data is declared over and over again, meaning any changes to the program become an infinitely more frustrating game of Where’s Waldo? as they hunt for duplicated data and functionality.

OOP is intimidating to a lot of developers because it introduces new syntax and, at a glance, appears to be far more complex than simple procedural, or inline, code. However, upon closer inspection, OOP is actually a very straightforward and ultimately simpler approach to programming

Why PHP?

Besides the fact that it is my programming language of choice, there are two reasons to start with PHP.

PHP is widely used

PHP is likely the most widely used server-side programming language (see this article, for example). It’s used on millions of servers world wide.

PHP is very popular in the open source world, where applications like WordPress and Drupal live. One reason is that PHP is itself open source.

This means that your PHP skills will have value to many different people (including you!).

PHP is (relatively) easy to learn

Make no mistake: programming is hard. For most people, anyway.

What people have trouble with is not the programming languages, but the way of thinking about program design. This only comes with practice, practice, and more practice.

But still, some languages are easier to learn than others. Java is one of the more difficult. It’s a good language, but it’s very structured, and hard for beginners to get started with.

Two of the easiest languages to learn are JavaScript and PHP. Even a few lines of JavaScript and PHP can do useful things. You can start writing programs quickly, without having to learn of lot of stuff first.

What you will need:

  1. A computer
  2. A development environment for PHP.

If you don’t have a development environment yet, never fear, you have 2 options.

  1. If you just want to play around and have reliable internet, Cloud 9 is a free online IDE (integrated development environment) that has everything you need and is quick and easy to set up. I have more detail explanation to set up Virtual development environment here.
  2. If you actually want to set up a LOCAL development environment on your machine please check out my detailed explanation here.
    This option is for you if:

    1. you want to be able to access you development environment offline
    2. you want more control over your environment
    3. you are a serious programming student and what to use an industry standard way of having development environments

Files used in class

Full PHP Script used in presentation (intro.php): https://github.com/sketchings/oop-basics

Lets Get Started

I’m excited to work with you all and building our skills together. If you have any questions or concerns, please let me know :-) If you are interested in taking this as a hands on workshop, I will be presenting this series at different venues in the Portland area, most are free, so please drop me an email and I will let you know about upcoming events.

Check out Part 1

Check out Part 2

Concusion / Evaluation

If you are interested in taking this as a hands on workshop, I will be presenting this series at different venues, mostly in the Portland area. Most are free, so please drop me an email and I will let you know about upcoming events.

If you HAVE taken the class, please fill out the evaluation so I can continue to improve this and other lessons.

Setup a Local Development Environment

This is designed as a guide to get you started with a php development environment on your computer.

  1. Download and install the latest version of VirtualBox from
    https://www.virtualbox.org/wiki/Downloads
  2. Download and install the latest version of Vagrant from
    http://www.vagrantup.com/downloads.html
  3. Download the file to set up your virtual machine here
    http://sketchings.com/wp-content/uploads/2015/02/puphpet.zip
    we will be using NGINX on Debian with PHP 5.6
  4. Extract the .zip file and move the contents (Vagrantfile and puphpet folder) to the location on your computer where you would like to store this project.
  5. Using the terminal, or cmd line, cd into your extracted directory and run $ vagrant up. This will kick-off the initial process.
    • Vagrant will download the box file, which can take a few minutes. It will only have to do this once, even if you create separate environments later on.
    • Then, it will hand control over to Puppet which will begin setting up your environment by installing required packages and configuring tools as desired.
    • You will then be able to ssh into your new box with $ vagrant ssh. You can also access any virtual hosts you created by editing your hosts file and creating entries for the Box IP Address and Server Name you provided during configuration (ex:192.168.56.101 oophp.dev www.oophp.dev).
    • To shut down the VM, simply run $ vagrant halt. To start it back up run$ vagrant up again. Destroy it with $ vagrant destroy.
  6. You will need to open and edit your hosts file with a text editor like notepad, sublime_text, nano, etc. The location of the hosts file varies by operation system.Windows users could look here: c:\windows\system32\drivers\etc\hostsLinux and Mac OSX users could look here: /etc/hosts or /private/etc/hosts.Example Entry: 192.168.56.101 oophp.dev www.oophp.dev