Forum Moderators: coopster
I'm reading the ZEND PHP cert study guide (not because I'm boing to take the test, but because I figure it lays out what I should know), watching the lynda.com video and researching online because I want to understand as much of the basics and proper practice of coding in PHP as I can.
But I really can't find out the difference between functions, classes, and objects. At least I can't get a clear definition of the three.
What I think I understand is that classes are collections of functions (ie..user defined functions). A class is like a seperate script within a script.
An object is a collection of classes?
It's amazing on how I associate things like this with forum software... If I had a forum, the object would be the submission form. The submission for would be made up of one or more classes that contained the functions that made it happen?
edit: can someone please edit the typo in the title? :)
I think I'm way off...help?
Thanks
A class is a collection of variables and the functions which operate on those variables.
An object is an instance of a class.
Using your forum analogy, you could have a user class, a thread class, and a post class.
The user class would contain variables like moniker, email address, join date, post count, and status.
The post class would contain variables like the post text, the creation date/time, number of times it's been viewed, its identifier, and a user object. It could also contain functions to display the post, create/edit the post, and for moderators, move or delete the post.
This very thread would be a thread object and would contain (now) two post objects.
You instantiate (make an instance of) a class when you need to use it. For example, when a user clicks on this thread you could create the two post objects, then in turn display them.
foreach($posts as $post)
$post->display();
<?php
class MyClass {
function MyClass() {
echo '5) This is the constructor and is echoe\'d when you instantiate the class.<br />';
}
function moreInfo() {
echo '6) This is a class function moreInfo.<br />';
}
}
echo '1) Is this thing working?<br />'."\n";
echo "2) $MyClass->moreInfo()<br />\n";
echo '3) Did $MyClass->moreInfo() do what you expected?<br />'."\n";
echo "4) MyClass::moreInfo()<br />\n";
$class = new MyClass;
$class;
$class->moreInfo();
echo '7) How about that?<br />'."\n";
echo 'Finally just to confuse things a little, you can use a class when you havent called new...<br />';
$info = MyClass::moreInfo();
echo $info;
?>
An object is a collection of classes?
Just to add... An object is an instance of just 1 class. A class is like the definition. When you instantiate it, you create an instance of that class in memory which is assigned to a variable (this is then an object). You can have many instances (variables/objects) of the same class.
A function defined inside a class is called a method of that class.
Generally you don't refer to the class directly although, as PHP_Chimp states above, PHP does allow you to do this in some cases with the double colon (::) syntax. Generally you instantiate it first.
The definitions of classes and objects apply to any object orientated programming language, although there might be slight differences in their implementation.