WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : Everything is an object
Categories : PHP, Object Oriented, Beginner Guides
Carlo Tasca
Carlo Tasca
Date : 2008-03-13
Grade : 2 of 5 (graded 11 times)
Viewed : 8465
Search : More Articles by Carlo Tasca
Action : Grade This Article
Tools : My Favotite Articles


  Submit your own code examples 
 


"We are all made of stars" sings Moby in one of his top-selling tracks, and how could I disagree with that?!



If there are about 7,000,000,000,000,000,000,000,000,000 atoms in a 70kg human body, and if we would consider atoms as objects, is everything an object, even us?



Trying to give an answer to this question can start a pretty long moral debate, but I can affirm surely that that's the way it is in object-oriented programming.



Thanks to the progress of abstraction, in OOP it is possible to take conceptually any real-life component and build a software representation of it.





For example, you can create a Dog object, a Car object, or you might as well create a Daddy object and so on.



Each one of these objects has in its basic definition both data and behaviour, being data in the form of attributes' values, and behaviour the actions it takes when receiving a particular request.



Having both data and behaviour is the key difference between object-oriented programming and the procedural approach. In well-designed object-oriented software, nothing outside the object can change directly the value of its attributes, and the only way to communicate with the object, or have it doing anything, is to send it a message.



We could at this point narrow down the definition of an OOP application as being a collection of objects that communicate with each other by sending messages.



A message is a request you send to an object in order to gain, give or process data.



When we say "send a message" to an object, what happens in reality is we make a call to a method which is part of the behaviour of that object.



The set of all the methods part of the definition of an object is often called 'protocol', and the collection of all the values' attributes of an object is often referred to as 'state'.



So we can say that an object has a state and a protocol, both of which had been defined in its abstract software representation.



Basic definitions (or abstract software representations) for an object in PHP (as well as in other languages) are provided via the keyword class.



The keyword class is not actually used to create objects ( this is achieved in another way), but to define the main template from which objects belonging to that class are created.



This makes perfect sense. Think about if you had to provide different code for any Dog object you need just because they have different names or because one is black and the other white.



Instead, thanks to the class keyword, we can define a software representation of a dog, with certain attributes and behaviour and create as many Dog objects as we want.



Each time we create a new Dog object we can give it different attributes values (e.g Fufi, black, etc) as well as being able to send to the object messages part of its protocol. Creation of new objects is achieved via the keyword new and we'd say that a new instance of that class has been created.



When defining a new class, we bring in a new type; this means we will be able later on to define a variable of that type, the same - or better, almost the same - as we do with numbers, strings and booleans.



You may know common data type such as an integers, double numbers, etc. These are data types available by default to a programming language and often called 'primitive data types'.






Variables that hold objects (instances of a class) are not primitive data types; they are reference data types.



The difference between primitive and reference data types lays in the fact a computer stores them in memory diversely.



Without going into too many details, the main difference is that for primitive data types the value assigned is stored directly in a block (or cell) of memory, while for reference data types the memory block simply holds an address that is a reference to the instance of a class.



I am going to conclude this article with a simple code example on how to define a class and create an instance of that class in PHP.


<?php
// class definition for Daddy
class Daddy
{
// code for Daddy class goes here
}

// declare $myDaddy and assign to it instance of Daddy class

$myDaddy = new Daddy();
?>


The code won't do anything so don't try it. What you need to focus on is simply how to create a template for an object via the keyword class and how you can create new objects of a class via the keyword new.



Note also the class definition has no round brackets while when creating an object we use round brackets after the name of the class. There is a reason for this and will be the subject of another article. Thanks for reading!









PHP 101 Part 7 of 15 : The Bear Necessities
Categories : PHP, Beginner Guides, Object Oriented, PHP Classes
PHP Classes And Objects: A Guide To Development
Categories : PHP, PHP Classes, Object Oriented, Beginner Guides
PHP and OOP
Categories : PHP, Object Oriented, Beginner Guides
Jump Start to Easy URLs
Categories : PHP, Beginner Guides, MySQL, File System, To PHP
PHP5: Designing And Using Interfaces
Categories : PHP, Object Oriented, Interfaces, PHP Classes, Security
Beginners guide to PHP and MySQL - Creating a simple guest book
Categories : Beginner Guides, To PHP, To MySQL, PHP, MySQL
Smarty Introduction
Categories : PHP, Smarty, Beginner Guides
PHP 101 Part 13 of 15 : The Trashman Cometh
Categories : PHP, Beginner Guides, Data Validation
Counting - Creating a simple counter
Categories : PHP, MySQL, Beginner Guides, To PHP, To MySQL
How To add paging (Pagination) with PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, HTML and PHP
Beginners Guide to PHP - Introduction to cookies
Categories : Beginner Guides, Cookies, To PHP, PHP
PHP 101 Part 8 of 15 : Databases and Other Animals
Categories : PHP, Beginner Guides, Databases
Making PHP Forms Object-Oriented
Categories : PHP, HTML and PHP, Object Oriented
PHP 101 Part 9 of 15 : SQLite My Fire!
Categories : PHP, Beginner Guides, Databases, SQLite
Beginners guide to PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, Installation