PHP FAQs and Stickies
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP DevelopmentPHP FAQs and Stickies

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 31st, 2011, 12:21 PM
Northie's Avatar
Northie Northie is online now
Square Peg in a Round Hole
Click here for more information.
 
Join Date: Oct 2007
Location: North Yorkshire, UK
Posts: 3,420 Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 10 h 29 m 44 sec
Reputation Power: 3896
An Introduction to Object Oriented Programming

Do not respond to this unless you have questions about the content of the examples. "Thank you" posts and general questions about OOP are not welcome on this thread

As requested by some members, and intended to be a useful resource to add to, here is an introduction to OOP.

This is by no means complete and is intended to help those who just don't get objects to think about things a little differently, and slowly introduce new concepts.

The next installment will look at object inheritance.


Pre-requisites
  • You currently write and understand PHP sufficiently well enough to know what variables and functions are in PHP
  • You are aware that PHP supports something called "objects" and that there is a concept called "Object Oriented Programming (OOP)"

These posts will help you build on your current understanding of PHP towards understanding what objects are and how they are used in a PHP programming environment.


Object Orientated PHP

If you are approaching objects from a procedural or functional background then the concept of an object in a programming context can seem confusing, bewildering, backwards... or any combination thereof.

In essence PHP is a procedural language with support for functions and objects. It is also a single threaded language so for every request a procedure begins, executes a set of commands, line by line, based on the code and then finishes.

Part of the object confusion is following the procedure. If you're trying to learn by inspection then you'll often finding your way jumping from file to file looking at functions inside classes using other objects in other files. This will drive you crazy and attempting to learn from the beginning in this way will quite often lead to headaches, frustration and resentment of OO principles.

Ideally you'll grasp the OO basics and principles, and then you'll start learning by inspection, testing and other sources, eg the PHP manual, PHP forums, etc. If you understand the concept, the syntax and the principles then you can move forward.

OO Nomenclature (naming):

Functions inside classes are called methods
Variables inside classes are called properties (sometimes also called attributes or fields)
A 'class' is the code you write, an 'object' is what you get when you use a class

PHP Code:
//this is a class
class {
    
/* code */
}

//$a is an object "of class A"
$a = new A


This code defines a class. When we do "new A" we get an object of class A or, in other words, "an instance of A".

At this point it might be worth looking at some dictionary definitions of "class" to see why the term is used

"Class":

Quote:
A set or category of things having some property or attribute in common and differentiated from others by kind, type, or quality.


Quote:
Assign or regard as belonging to a particular category:


This starts to make sense a little bit until we look at the term object

"Object":

Quote:
A material thing that can be seen and touched.


Huh? Hopefully this is what this article will answer!

"Instance":

Quote:
An example or single occurrence of something


Classes and Objects help you divide up and manage your code better. At a basic level they offer an extended scope and can reduce copying and pasting of code - ie you can improve your code reuse.

All OO languages have something called the "object operator". in php this is "->", eg


PHP Code:
//this is a class
class {
    function 
foo() {
        echo 
"Hello World";
    }
}

//$a is an object "of class A"
$a = new A;

//here we access one of the object's methods
$a->foo(); //outputs "Hello World" 


in this example $a is an object, to access it's method "foo", we use the object operator, ->. To the left is an object, to the right is one of the object's methods or properties

When writing a class, you'll be thinking about how the class behaves as an object. You'll be assuming something else has created an instance of your object and the flow of logic and control within use. When coding like this you'll want to access other methods or properties within your class. To do this you assume the variable "$this" as the current instance of the object, so you may do something like this

PHP Code:
//this is a class
class {

    private 
$str;
    
    public function 
__construct() {
        
$this->str "Hello World";
    }

    public function 
getStr() {
        return 
$this->str;
    }
}

//$a is an object "of class A"
$a = new A;

//here we access one of the object's methods
echo $a->getStr(); //outputs "Hello World" 


In this example we have introduced "$this" (as discussed), "__construct()", "public" and "private"

the __construct() method allows you to set stuff up. this is called automatically when you create an instance of the object (ie when you do "new ClassName"). In this example we've used the constructor to set the property $str to "Hello World". When we call getStr, we then get "Hello World" in return. Hopefully this demonstrates the extension of scope.

"private" and "public" are two forms of visability. "private" methods and function can only be accessed inside the class code by using $this->nameOfPrivateThing. Public methods and objects are accessible to things outside the class definition. A third type "protected" will be addressed later when we talk about class inheritance and extending classes and objects.

If we now revisit the definition of object ("A material thing that can be seen and touched"), then consider this $a is an object of class A, or an instance of A. our code then touches parts of it, such as the methods or properties. we "touch" it by using the object operator, ->.

Why do we want to touch or feel objects?

There are two main reasons

To set the state of the object (ie provide input settings)
To measure the state of the object (ie get information/output from it)

A third reason may be to invoke a process, which falls part way between these two main reasons, we may invoke a process that changes the state of the object and returns data... or each part may have its own method

A common part of most PHP coders library is a database class. In a simple set up the constructor will create a connection to the database. Another method may execute a query (ie set the SQL property, execute the query and hold the result in another property). A third method might return the data. eg

PHP Code:
class DB {
    
    private 
$conn;
    private 
$originalSQL;
    private 
$protectedSQL;
    private 
$data;

    public function 
__construct() {
        
$this->conn = new DataBaseConnection($host,$user,$pass,$database); //ooh look, we've just created a new instance of a connection object. This isn't defined here at all but we will assume we know how it works
    
}
    
    public function 
Execute($sql) {        
        
$this->setSql($sql);
        
$this->protectSQL();
        
$this->data $this->conn->query($this->protectedSQL);
    }
    
    private function 
setSql($sql) {
        
$this->originalSQL $sql;
    }
    
    private function 
protectSQL() {
        
$this->protectedSQL $this->conn->prepare($this->originalSQL); //assume that DataBaseConnection has an escaping or protection method
    
}
    
    public function 
getData() {
        return 
$this->data;
    }
}

//Which we might use like this:

$db = new DB//calls constructor, which creates the connection

/* Every time we do new DB we will create a new DB object and as a result of that we will get a new DataBaseConnection object each time as well. This is probably not wanted as multiple connections will consume unnecessary memory. The singleton design pattern could solve this*/

//execute a query
$db->Execute("SELECT * FROM myTable");

//get the data
$result $db->getData();

//display the data
print_r($result); 


In this example we have a used all private properties, so none of the properties can be accessed from outside the class. we cannot use "$db->data" directly, we retrieve the information through a "getter": getData(). A method may return the data stored in private properties or the result of a private method.

The Execute method invokes the process of
* changing the state of originalSQL, by setting it to the supplied string)
* changing the state of protectedSQL by setting it to the value returned by $this->conn->prepare (which in turn invokes a set of processes within the DataBaseConnection object)
* changing the state of data by setting it to the value returned by $this->conn->query

The getData method returns the state of (or data in) $this->data

Next: Object inheritance and extending
__________________
PHP OOPS! <?php DB::Execute(SQL::makeFrom($_GET))->fetchArray()->FormatWith(Template::getInstance('default'))->printHtml(); ?>

PDO vs mysql_* functions: Find a Migration Guide Here

[ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]

Last edited by requinix : April 12th, 2012 at 04:54 AM. Reason: added prerequisites section

Reply With Quote
  #2  
Old October 31st, 2011, 05:24 PM
Northie's Avatar
Northie Northie is online now
Square Peg in a Round Hole
Click here for more information.
 
Join Date: Oct 2007
Location: North Yorkshire, UK
Posts: 3,420 Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 10 h 29 m 44 sec
Reputation Power: 3896
Extending Objects

Consider this code
PHP Code:
class {
    public 
$O;
    public 
$P;
    
    public function 
foo () {
    
    }
    
    public function 
bar () {
    
    }

}

class 
extends {
    public 
$X;

    public function 
foo () {
    
    }

    public function 
baz () {
    
    }

}

class 
extends {
    public 
$Y;

    public function 
foo () {
    
    }

    public function 
car () {
    
    }

}

class 
extends {
    public 
$Z;

    public function 
foo () {
    
    }

    public function 
day () {
    
    }




Classes B, C and D extend class A. This means is that classes B, C and D inherit properties and methods of A. Diagramatically we could represent the classes like this

Code:
+-------------------+
| Class A           |
+-------------------+
|                   |
| Property O        |
| Property P        |
|                   |
| Method foo() {}   |
| Method bar() {}   |
|                   |
+-------------------+

+-------------------+	+-------------------+	+-------------------+
| Class B Extends A |	| Class C Extends A |	| Class D Extends A |
+-------------------+	+-------------------+	+-------------------+
|                   |	|                   |	|                   |
| Property X        |	| Property Y        |	| Property Z        |
|                   |	|                   |	|                   |
| Method foo() {}   |	| Method foo() {}   |	| Method foo() {}   |
| Method baz() {}   |	| Method car() {}   |	| Method day() {}   |
|                   |	|                   |	|                   |
+-------------------+	+-------------------+	+-------------------+

Could be represented like this
Code:
+-------------------+	+-------------------+	+-------------------+
| Class A           |	| Class A           |	| Class A           |
+-------------------+	+-------------------+	+-------------------+
|                   |	|                   |	|                   |
| Property O        |	| Property O        |	| Property O        |
| Property P        |	| Property P        |	| Property P        |
|                   |	|                   |	|                   |
| Method foo() {}   |	| Method foo() {}   |	| Method foo() {}   |
| Method bar() {}   |	| Method bar() {}   |	| Method bar() {}   |
|                   |	|                   |	|                   |
+-------------------+	+-------------------+	+-------------------+
| Class B Extends A |	| Class C Extends A |	| Class D Extends A |
+-------------------+	+-------------------+	+-------------------+
|                   |	|                   |	|                   |
| Property X        |	| Property Y        |	| Property Z        |
|                   |	|                   |	|                   |
| Method foo() {}   |	| Method foo() {}   |	| Method foo() {}   |
| Method baz() {}   |	| Method car() {}   |	| Method day() {}   |
|                   |	|                   |	|                   |
+-------------------+	+-------------------+	+-------------------+

which, in turn, is equivilent to this, and this is how the PHP engine sees it
Code:
+-------------------+	+-------------------+	+-------------------+
| Class B           |	| Class C           |	| Class D           |
+-------------------+	+-------------------+	+-------------------+
|                   |	|                   |	|                   |
| Property O        |	| Property O        |	| Property O        |
| Property P        |	| Property P        |	| Property P        |
| Property X        |	| Property Y        |	| Property Z        |
|                   |	|                   |	|                   |
| Method foo() {}   |	| Method foo() {}   |	| Method foo() {}   |
| Method baz() {}   |	| Method car() {}   |	| Method day() {}   |
|                   |	|                   |	|                   |
+-------------------+	+-------------------+	+-------------------+


Where this is used.

class inheritance is most commonly used for polymorphism....class A holds lots of generic code that you don't want to rewrite or maintain in more than one place

The "foo()" method of A is overwritten by the foo methods of B, C and D. the extending classes don't have to overwite any of the methods, but can do if you wish.

Quite often the __construct() method is overwritten. In such cases you may also want the contructor of A to be executed, which it would not be if you wrote "$b = new B;". To fire the constructor of the parent class, we use just use this

"parent::__construct();" like this

PHP Code:
class {
    public function 
__construct() {
    
    }
}

class 
extends {
    public function 
__construct() {
        
/*You can put code here*/
        
        
parent::__construct();
        
        
/*You can put code here*/
    
}



Ooh, look now we've introduced "parent" and "::". "parent" is a PHP keyword that means the parent class, in this case, class A.

The scope resolution operator, :: is generally used to statically call class methods, out of object context as if they were general functions. When used with PHP keywords that refernce is slightky diferent and references the currently in-use class (not object). Such keywords can be "self" or "parent" (consult the manual for more information). Although not officially documented as such, I think of "->" as the object operator and "::" as the class operator. Spend some time thinking back to the difference between clases and objects and hopefully this subtle difference will become more distinct.

Moving beyond basic inheritance

In many large scale and commercial projects there will be multiple developers. In such environments authors of classes will often design classes that can be extended by other developers to create the functionality they need, with a common "library" held in the originall class. In a lot of these cases it will not be desired that the original class be instantiated, or even allow some methods to overwitten.

Toi aid with this PHP gives us "abstract" classes and "final" methods

An abstract class may not be instantiated with the "new" keyword. A "final" method may n ot be overwitten. Consider:

PHP Code:
abstract class {
    
    public function 
foo() {
    
    }
    
    public final function 
baz() {
    
    }
    
}

class 
extends {

    public function 
bar() {
    
    }
    
/*
    //not allowed, will cause error as baz has been declared final in A
    public function baz() {
    
    }
    */
    
}

$a = new A//not allowed, will cause error

$b = new B;

$b->foo(); //functionality written in A::foo
$b->bar(); //functionality written in B::bar
$b->baz(); //functionality written in A::baz 


Lets now assume that the original author of A intended that all classes extending A should have a method "bar" that is designed to be used by an external class to access various parts of the object in a unified way but the code inside bar() could be so diverse that there's no point writing it in A.

PHP gives us interfaces. An interface defined which methods should be implemented. If we take the above code and add an interface we get this:

PHP Code:
interface iA {
    public function 
bar();
}

abstract class 
implements iA {
    
    public function 
foo() {
    
    }
    
    public final function 
baz() {
    
    }
    
}

class 
extends {

    public function 
bar() {
    
    }
    
/*
    //not allowed, will cause error as baz has been declared final in A
    public function baz() {
    
    }
    */
    
}

$a = new A//not allowed, will cause error

$b = new B;

$b->foo(); //functionality written in A::foo
$b->bar(); //functionality written in B::bar
$b->baz(); //functionality written in A::baz 


class A implements iA but does not define bar, but that's okay because we can't do "new A" because it's abstract. We can do "new B" which extends A which implenents iA, so class B must implenebt method bar() or else the code will error.

Next we'll take a break from OOP (unless I think of something I've forgotten that I wanted to say) and have a look at the manual, and how useful it can be.

If you're lost or even more confused now than before please please please ask now. One big part of not getting OOP is not understanding why it came about. If you can figure out why the need for OO arose - what the problems are the OO address, then it will help you greatly grasp the concepts of how OO supplies the solution

Reply With Quote
  #3  
Old November 2nd, 2011, 10:02 PM
zxcvbnm's Avatar
zxcvbnm zxcvbnm is offline
A Change of Season
Click here for more information.
 
Join Date: Mar 2004
Posts: 1,600 zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 7 m 45 sec
Reputation Power: 70
Very good post. Useful tips I hadn't found elsewhere. 2 things:

1 - Please continue with these tips
2 - Please define DataBaseConnection for your example above.

Many thanks
__________________
Devshed people, please fix the spell check:

System is temporarily busy. Please try again in a few seconds.

Reply With Quote
  #4  
Old November 3rd, 2011, 03:55 AM
paulh1983 paulh1983 is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2004
Posts: 2,234 paulh1983 User rank is First Lieutenant (10000 - 20000 Reputation Level)paulh1983 User rank is First Lieutenant (10000 - 20000 Reputation Level)paulh1983 User rank is First Lieutenant (10000 - 20000 Reputation Level)paulh1983 User rank is First Lieutenant (10000 - 20000 Reputation Level)paulh1983 User rank is First Lieutenant (10000 - 20000 Reputation Level)paulh1983 User rank is First Lieutenant (10000 - 20000 Reputation Level)paulh1983 User rank is First Lieutenant (10000 - 20000 Reputation Level)paulh1983 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 10 h 37 m 56 sec
Reputation Power: 201
Please define DataBaseConnection for your example above.

maybe you could do that yourself i.e try it yourself and then post on a SEPARATE thread

Reply With Quote
  #5  
Old November 3rd, 2011, 04:16 AM
Northie's Avatar
Northie Northie is online now
Square Peg in a Round Hole
Click here for more information.
 
Join Date: Oct 2007
Location: North Yorkshire, UK
Posts: 3,420 Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 10 h 29 m 44 sec
Reputation Power: 3896
Quote:
Originally Posted by zxcvbnm
1 - Please continue with these tips


I fully intend to, but am a busy person at the momment

Quote:
Originally Posted by zxcvbnm
2 - Please define DataBaseConnection for your example above.


No.

This is not a refusal, there is just no need. As you can see from the code, I've only referenced two methods,

DataBaseConnection::query
DataBaseConnection::prepare

The class doesn't exist anywhere but in mind-space, This is a thought experiment and none of these classes really exist...anywhere

A big part of OO is that you don't need to know how it's defined, just what public methods are available, and what they return - which I hoped was evident from the use of these methods.

As paulh1983 suggested, you could try to implement your own (In fact I've emailed you enough to be getting on with that in the past)...but as this is a tips/tutorials post I'd also ask that it go in a separate thread

Reply With Quote
  #6  
Old November 7th, 2011, 11:13 AM
Northie's Avatar
Northie Northie is online now
Square Peg in a Round Hole
Click here for more information.
 
Join Date: Oct 2007
Location: North Yorkshire, UK
Posts: 3,420 Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 10 h 29 m 44 sec
Reputation Power: 3896
The PHP Manual

English Version: http://www.php.net/manual/en/

For other languages choose www.php.net > documentation > [English | Brazilian Portuguese | Chinese (Simplified) | French | German | Japanese | Polish | Romanian | Persian | Spanish | Turkish]

The manual is very well written and very well presented, it's a long way from it's humble beginnings and the manual for v3 (if you can find it, have a look at it). Each page has a user contributed notes section which can be very useful if at first you don't understand the native PHP documentation

However, If you don't understand what it's talking about then you'll struggle to work yourway through it. I'm going to try to concentrate on the "Language Reference > Classes and Objects" Section, http://www.php.net/manual/en/language.oop5.php. I am assuming you understand the majority of all previous sections

For now, you can ignore the introduction - it will only introduce more terminonly which you are not yet comfortable with, so skip stright to "The Basics". Here you will be introduced to several fundemental keywords for objects, including these five from the above examples:

class
new
extends
$this
::

The Basics chapter also covers

self
parent
static

Some of these terms you will have seen before, others you may not. Recognition and comprehension are not necessarily the same thing and the basics page covers "class", $this, "new" and "extends".

The scope resolution operator or "Paamayim Nekudotayim" (::) has its own page at http://www.php.net/manual/en/langua...nekudotayim.php. It may also be worth reading the static page at this point as well. Skim over it so you are aware of it, then go revisit it while working through the manual

the parent keyword allows access to methods and properies in parent classes that have been overridden by the current class, eg parent::__construct(). The use of parent and self with :: is a way to statically call methods/properties without leaving the object context. At first, this may seem an odd feature to have... "Why would you have such a thing?"...The answer is that at some point you'll need it. If you haven't come accross the need to use it so far, then wait until you do. TBH, I don't fully understand the logic behind the syntax, but I do understand what the syntax does (just).

The left-navigation of the classes and object section aims to walk you through the Object Oriented approach taken by PHP 5+. Be aware that not all features are availabile in all version of PHP5, for example Namespaces were introduced in 5.3, and traits in 5.4.

A basic level of knowledge is required to work through these pages, which I hope you now have

Reply With Quote
  #7  
Old January 6th, 2012, 03:00 AM
zxcvbnm's Avatar
zxcvbnm zxcvbnm is offline
A Change of Season
Click here for more information.
 
Join Date: Mar 2004
Posts: 1,600 zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 7 m 45 sec
Reputation Power: 70
Quote:
Originally Posted by hua052011
I do not agree above ideal. We can find out some articles at about.com by using Google search.


This is one of the most comprehensive threads I have read on OOP. Can't wait for more posts on it.

But thanks for the tip on Google search.

Reply With Quote
  #8  
Old March 12th, 2012, 11:21 AM
Northie's Avatar
Northie Northie is online now
Square Peg in a Round Hole
Click here for more information.
 
Join Date: Oct 2007
Location: North Yorkshire, UK
Posts: 3,420 Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 10 h 29 m 44 sec
Reputation Power: 3896
traits

This was going to be about interfaces, but while trying to write about interfaces in an easy to understand way (and get my head round the full power of them) PHP 5.4 was released with a new feature - traits

If you've got access to php5.4 or want to persuade your host to upgrade then read the manual page and this thread

Reply With Quote
  #9  
Old March 12th, 2012, 12:04 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,805 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 17 h 43 m 13 sec
Reputation Power: 6112
Interestingly, you could imitate traits using a "bug" (not really a bug but definitely an odd feature) in PHP well before this. If you're inside a class and you statically call a method on another class which contains $this, the $this will refer to the FIRST class you're in:
PHP Code:
class classA {

  public 
$data = array(1,2,3);
  
  public function 
__construct() {
  
    @
classInterface::doPrint();
    
  }
  
}

class 
classB {

  public 
$data = array('a','b','c');
  
  public function 
__construct() {
  
    @
classInterface::doPrint();
    
  }
  
}

class 
classC 

  public 
$data = array( 'books' => array (
    array( 
'author' => 'George R R Martin''title' => 'A Game of Thrones' ),
    array( 
'author' => 'Douglas Adams''title' => 'The Hitchhiker\'s Guide to the Galaxy' ),
    array( 
'author' => 'Neil Stephenson''title' => 'Snow Crash' ),
    )
    );
  
  public function 
__construct() {
  
    @
classInterface::doPrint();
    
  }
}

class 
classInterface 

  public function 
doPrint$data ''$indent ) {
  
    if ( 
$data === '' ) {
      
$data $this->data;
    }
    foreach ( 
$data as $key => $element ) {
    
      echo 
str_repeat("\t"$indent) . "Element {$key} is (" gettype($element) . ") ";
      if ( 
is_array$element ) ) {
        echo 
"\n";
        
self::doPrint$element$indent+);
      } else {
        echo 
"{$element}\n";
      }
    }
  }
}



$a = new classA();
$b = new classB();
$c = new classC(); 
The code above produces:

Code:
Element 0 is (integer) 1
Element 1 is (integer) 2
Element 2 is (integer) 3

Element 0 is (string) a
Element 1 is (string) b
Element 2 is (string) c

Element books is (array) 
	Element 0 is (array) 
		Element author is (string) George R R Martin
		Element title is (string) A Game of Thrones
	Element 1 is (array) 
		Element author is (string) Douglas Adams
		Element title is (string) The Hitchhiker's Guide to the Galaxy
	Element 2 is (array) 
		Element author is (string) Neil Stephenson
		Element title is (string) Snow Crash
It shouldn't. It can't. classInterface doesn't have access to the member variables of classA, B, or C. In fact, classInterface is never instantiated so using $this in non-object context should produce a fatal error. But it doesn't.
__________________
HEY! YOU! Read the New User Guide and Forum Rules

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

Reply With Quote
  #10  
Old April 11th, 2012, 07:50 AM
Darknite Darknite is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: London
Posts: 40 Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 10 h 51 m 51 sec
Reputation Power: 14
A different approach

Hello Northie,

My First post here.

An interesting explanation of OOP, however I feel you have fell into the classic problem of "Quine based analogy". I.e. in trying explain OOP, you have used examples that already require one to know OOP already!

This is not a dig at your article, it is very detailed indeed.

I've started a new forum, its in its infancy.

However we have had a similar question regarding OOP;

Here is my approach to explaing Object Oriented Programming

Reply With Quote
  #11  
Old April 11th, 2012, 05:41 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,701 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 5 h 23 m 48 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Quote:
Originally Posted by brijeshmkt
i am php trainer. How popular is oops among developers. Is community prefering oops over scripting.

Oopsies are bad and, unfortunately, popular.
OOP is good and less popular. Once you know PHP well enough you should learn how to use object-oriented programming in it. That's one of the reasons we have this thread: for people to learn from.

Reply With Quote
  #12  
Old April 11th, 2012, 05:44 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,701 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 5 h 23 m 48 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Quote:
Originally Posted by Darknite
An interesting explanation of OOP, however I feel you have fell into the classic problem of "Quine based analogy". I.e. in trying explain OOP, you have used examples that already require one to know OOP already!

...

Here is my approach to explaing Object Oriented Programming

How about reposting it here? Because this is a sticky we're a bit pickier about what posts show up in it, and a post saying "go over to my forum" isn't quite what we want to keep around.

If you'd like to post your own information then please do. (Might want to wait until your second part is done though.) I think Northie is aiming for about OOP as it pertains to PHP so can you try talking about that specifically?

Fair warning: I'll wait a few days or so and prune both your and my post. Nothing personal, just trying to keep this thread clean.

Last edited by requinix : April 11th, 2012 at 05:46 PM.

Reply With Quote
  #13  
Old April 12th, 2012, 03:20 AM
Northie's Avatar
Northie Northie is online now
Square Peg in a Round Hole
Click here for more information.
 
Join Date: Oct 2007
Location: North Yorkshire, UK
Posts: 3,420 Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 10 h 29 m 44 sec
Reputation Power: 3896
If we're going to get pedantic, then I would suggest adding a pre-requisite list to the top of the first post (something I can no longer edit?!?!?) that reads something like

Pre-requisites

  • You currently write and understand PHP sufficiently well to know what variables and functions are in PHP
  • You are aware that PHP supports something called "objects" and that there is a concept called "Object Oriented Programming (OOP)"
This post will help you build on your current understanding of PHP towards understanding what objects are and how they are used in a PHP programming environment

Reply With Quote
  #14  
Old April 12th, 2012, 03:37 AM
Darknite Darknite is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: London
Posts: 40 Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 10 h 51 m 51 sec
Reputation Power: 14
Hello Guys,

@requinix, yes, you are correct, once I post the second half of that article over there, I will repost it here as well.

@Northie, Hi Northie, I was not being pedantic, merely saying that those who find it difficult to understand OOP have varying levels of base understanding. We slighlty more experience journey men often "forget" what it was like before we had our own "ah-ha" moment.

In an attempt to try and explain things, I find it best to start from the "ground-up". Of course those who are familiar with "variables" re-reading it is not a problem (they already know).

So in short, yes I will re-post here the completed article when I have done part II.

EDIT:

Just remembered: while your article is focused on OOP in PHP. My aim was more generalised than that. The assumption is once you have a solid understanding of the concept of OOP. You can then easily translate it to ANY language.

Reply With Quote
  #15  
Old April 12th, 2012, 03:47 AM
Northie's Avatar
Northie Northie is online now
Square Peg in a Round Hole
Click here for more information.
 
Join Date: Oct 2007
Location: North Yorkshire, UK
Posts: 3,420 Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 10 h 29 m 44 sec
Reputation Power: 3896
I really don't think I could make my introduction any simpler...to the point where I don't actually think you read it....because if you had read it you would have realised that all bases are covered: A term is thown in and then explained. I have assumed that the reader would be confused and then attempted to explain away the confusion - it's how I teach - i get people to think for themselves

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesPHP DevelopmentPHP FAQs and Stickies > An Introduction to Object Oriented Programming

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap