Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Getting Started with OOPHP - Real Life Example

Posted on 2014-10-08
7
139 Views
Last Modified: 2014-10-09
I've been writing procedural PHP for about six years now. My team and I have decided to make the conversion over to OO for several reasons.  This being said, I see a lot of examples about creating classes for animals, trains, planes, etc.  I have yet to find a good practical real life example that I could learn from.

I wrote a class below that I would use in a few different applications.  I'm not sure if I'm on the right track with this or not, but if somebody wouldn't mind going over what I've done and give suggestions, It would be greatly appreciated.

A little back story for how this will be used, if I understand OO right.  My company has two different types of accounts.  Prospects, who have never done business with us before, and Customers, who have placed an order at some point.

Most of our programs interact with our main companies table at some point, and several of which call functions from within this class.  Most applications have a link to create or edit a contact, and a prospect account can also be created from several spots.

While I only have one script for each (edit contact, create contact, add a prospect), I'm thinking that I can call this class from where I would link to perform said functions.

I'm not sure if what I posted below is a good start or example of what should happen with object oriented code, or if there is a better way to go about it based off of the information provided.

<?php

class Account() {
	//Define Variable
	var $compId;

	//Used to create a customer account 
	public function __construct() {
		//Not sure what this is...will it be used for $account = new Account();
	}
	
	//Pulls Account Data For AR Account
	function getArAccount($compId) {
		//This pulls all the data about the account and puts it in tabs
	}
	
	//Pulls Contact Management Search Results
	function getCMSearchRes($compId) {
	//This pulls account #, name, address, city, state, zip, country
	}
	
	//Pulls Contact Management Page Details
	function getCMCompDetails($compId) {
		//This pulls account #, name, address, city, state, zip, country, county, sales rep, sic, referral, email address, website
	}
	
	//Create a customer account 
	function createProspect() {
		//sql to handle post variables?  what about errors??
	}
	
	//Function to update prospect information
	function updateProspect() {
		//sql to handle updating the prospect information
	}
	
	//Creates a prospect account
	function createCustomer() {
		//sql to handle post variables?  what about errors??
	}
	
	//Function to update the customer data
	function updateCustomer() {
		//sql to update the customer information
	}
	
	function getContacts($compId) {
		//pulls all company contacts
	}
	
	function addContact($compId) {
		//post variables, error handling for adding a contact
	}
	
	function updContact($compId) {
		//post variables, error handling for updating a contact
	}
}

Open in new window

0
Comment
Question by:t3chguy
7 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 40369501
You're doing the right thing.  Welcome to the light!  The first thing you want to understand are "design patterns."  This is the canonical reference:
http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612

Some valuable learning resources...
http://www.amazon.com/PHP-Object-Oriented-Solutions-David-Powers/dp/1430210117/
http://www.amazon.com/Objects-Patterns-Practice-Experts-Source/dp/143022925X/ (Mandatory reading!)
http://www.phptherightway.com/ (Excellent and up-to-date.)

You probably want to read the entire PHP OOP manual.  Don't be discouraged if it sounds a bit arcane at first.  And don't worry too much about the details of the programming language.   You want to know the major concepts and where to look for the details of implementation.  Then when you're learning about object oriented design you can refer back to the concepts you learned from the OOP manual.
http://php.net/manual/en/language.oop5.php

Object oriented programming is one thing -- but good object oriented design is an entirely different thing.  When we do OO design we think mostly about the interface between objects.  The programming usually follows easily if the interfaces are well designed.  If you have some time to study and want to learn from one of the best, find this guy and absorb everything he will share with you.
http://www.brandonsavage.net/

About the code snippet - you can dispense with "var" because that's something that is leftover from PHP4.  We don't do that any more; instead we declare the properties of the object as public, protected or private.  The methods are declared the same way.  There are two good articles here at E-E that teach some of the basic principles.

http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_2626-Beginning-Object-Oriented-Programming-in-PHP.html

http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_2631-Advanced-Object-Oriented-Programming-in-PHP.html
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 250 total points
ID: 40369676
If you want a real world example, I wrote an article a few years ago here on EE

http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_3212-Using-PHP-classes-to-group-data-in-memory.html

I hope you find it helpful
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40370535
Almost forgot about it, but here is another article with real-world examples of OOP design and notation.  It computes "average" locations from a collection of geocodes.  Read the ones from @gr8gonzo and @bportlock first!
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_9854-Using-GeoCodes-to-Find-an-Average-Location.html
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Closing Comment

by:t3chguy
ID: 40370586
Thank you so much.  I'm sure I'll have more questions along the way, but glad to know I at least have a good starting point.  I'll definitely check out all references listed above!

Thanks again!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40370620
Thanks for the points -- it's a great question.  Don't be in too much of a hurry to learn this stuff; it's a whole new way of thinking about application design and programming and it will not come to you overnight.  Give yourself some time and make simple code examples (maybe keep a library of your code samples) along the way.  You'll get it, slowly at first, then all of a sudden, and as you do, you'll be amazed at the ways it changes your thinking about programming.  One of the interesting benefits is the amount of time you spend debugging.  It will fall to almost zero because your code will be so much simpler and you can automate your test process.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 40370826
Thank you for the points. I agree with Ray's point about debugging time. I need to spend so much less time on it these days because forcing myself to get the OO design right gets me out of most of the problems I would otherwise program myself in to.

If I have a complaint about OO it is that coding becomes very boring and (mostly) childishly simple.
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 40371645
+1 to the question and the responses.  This is a great reference thread.

Cd&
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question