| Welcome to zetaNetwork. We hope you enjoy your visit. You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as viewing zetaNetwork exclusive tutorials and articles, and access to our code snippets section. Registration is simple, fast, and completely free. Click Here To Register If you're already a member please log in to your account to access all of our features: |
| OOP For Noobs - Part 1 | |||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Tweet Topic Started: May 26 2011, 07:10 PM (186,556 Views) | |||||||||||||||||
| Pro | May 26 2011, 07:10 PM Post #1 | ||||||||||||||||
|
Underground Coding
![]()
|
OOP For Noobs - Part 1 Time: 1 Hour 30 Minutes Difficulty: Medium Note: This tutorial is for those of you who have at least a basic knowledge of PHP and wish to improve your code and your workflow. OOP in PHP is available in PHP5 only. If you're one of those people who still use procedural, wow. Anyway, OOP or Object Oriented Programming is probably one of the best things in programming, the fact that it even came to PHP is like heaven on Earth. OOP is more than just learning new syntax however, it's a concept of programming that may be difficult to grasp at first but very powerful once you get used to it. For those of you who don't know what procedural looks like: That's procedural bogging down your applications with loads of variables and other stuff you don't need. Using objects helps keep your neat and organized. Not to mention, you can have your own type of variables, well sorta. This tutorial will consist of 7 parts:
That block of code looks scary right? Well it's not we'll be breaking it down line by line so don't worry, but first, let's go over what an object is: An object is basically a form of a class (see below). Each object is unique and holds it's own attributes and functions. Now let's get started cracking down on that block of code.You should know what that means. That line declares a new class. What's a class? A class is basically a blueprint for your object. Think about a class as a blueprint for a house. A house has various attributes, such as size, color, etc. A house also has various functions such as allowing you to live in it. An object is just like a house. It can have attributes and functions, plus it can even have a constructor which is like building your house, or object in this case. In this case, our class name is Forum. $name is an attribute of our Forum class. Attributes can basically be anything a variable can be, integers, strings, reference variables. See "private"? The word private is a visibility modifier. Visibility modifiers basically tell your application what can access that specific attribute/function and what can't. A list of visibility modifiers are as follows:
Here's a nice table if you need them in more detail:
Note: Class attributes and functions are public unless specified otherwise. These visibility modifiers also allow for encapsulation. What's encapsulation? Encapsulation is basically when only things relevant to the current object are accessible to the object. If you had for example a human object. You don't want to know how the human walks, talks, eats, or sings. You just want the human to walk, talk, etc. Encapsulation makes it so you don't need to know how something works, you just want it to work. What's this, a function with two underscores before it? This is the constructor, think of it as the order to create a new house. It sets everything that is needed in the object, instance variables, etc. It essentially, contains code that needs to be executed when an object is created. The constructor is automatically called on the creation of a new object and is always public. A lot of people seem to get confused by this so I'm going to go over this in depth. $this refers to the current object instance. The arrow refers to something inside the current object, in this case the name variable. Note how you don't need a $ after name. Then you see = $name, this means that the name attribute of our object is being set to $name, which is passed through our constructor. Wait, did we just set the name attribute of our attribute to itself? No, we did not. $this->name refers to the name attribute in our class. $name refers to the string that was passed in our constructor. Wait, what's an instance? An instance is basically another name for an object. Each Forum object basically has it's own personal name attribute, unless $name is declared static. By putting the word static in between private and $name, $name will have the same value across all forum objects. Meaning if you change the value of name for one Forum object you change the value of name for all instances of Forum. To access a static attribute or function from within an object use self::your_attribute_or_function(). That line of code basically say, create a new function called get_name that is accessible by everything. The function will return the name attribute of our object. Add in one more closing bracket and we're done with our class. Now let's move on to the next part of the code, which creates two Forum objects. Create a new variable called forum1, forum1 will be a reference variable to a new Forum object. Notice how we passed a forum name to our constructor. This sets the name of our Forum object to "IT'S OVER 9000". Do the same thing with $forum2 except with a different name and there you have it, two forum objects. Now let's print the names of our forums. Echo the string that is returned by get_name() and there you have it. We've gone through all the code in our sample. Now let's get a bit more in depth with OOP now that we've gone over the basics. Recall how I mentioned subclasses with visibility modifiers. What's a subclass, a subclass is basically a class that inherits all the public and protected attributes and functions of it's parent class. What's inheritance? Inheritance is one of those concepts and parts of OOP that helps you save time maximizing efficiency. For example, say you had a Computer class, a Desktop class, and a Laptop class. Without inheritance you would have to define an attribute, such as processor in every single class. Inheritance helps us save time by allowing a class to be a subclass of another class meaning you only need to declare the processor attribute as public or protected in the parent class and all subclasses of that class will have the attribute processor. It is due to inheritance that people will usually create a very generic class containing attributes and functions that multiple classes will use and create subclasses of that class gradually getting more and more specific with their classes. Note: Everything, even the constructor can be inherited. Here's some sample code to show you how to use inheritance. Whoa, that's a large block of code. Let's take it easy, class by class, line by line. We'll start with the Computer class which contains three protected attributes and a constructor. Our Computer class will hold the protected variables processor, storage, and os. All of them will be set from within the constructor. Within our constructor we set our class attributes to their corresponding values.
We'll also declare a get_stats function that returns the stats of our computer. Now for the good stuff ![]() You're probably looking at this code and thinking, "What the hell does it do?" Well for starters look at the class heading, "extends Computer", that means that this class, Laptop, is a subclass of computer. All the public and protected attributes and functions are inherited by Laptop. "But if it's inherited why do you need to redeclare the constructor and get_stats?" You may need to redeclare the constructor, because Laptop is a different class, but you don't need to redefine get_stats. The reason we are redefining it, though, is because the get_stats function in our Computer class doesn't print out the battery attribute in our Laptop class. By defining get_stats in our Laptop class we are overriding the get_stats function in Computer and therefore $this->get_stats() will call the get_stats function from Laptop and not Computer. You already know what the constructor header does and what $this->battery = $battery does. Now we're gonna go over what "parent::" does. The keyword parent means the parent of our Laptop class, in this case, Computer. The line "parent::__construct(...);" means that we are calling the constructor of our parent class. Yep, that's right, the constructor of a parent class is not automatically called when a child class, or subclass, is instantiated. Wait, the class attributes processor, storage, and os were not defined in our class!!! Actually, they were, when we extended our class our class inherited all the attributes of Computer, including processor, storage, and os. The same thing goes for our Desktop class.
Well there you have it, part 1 of OOP For Noobs. I know, long right? In the next part of this tutorial series we'll be going over abstract classes and interfaces. If you have any questions or comments post them here and remember, you can always download the source files in the attachment below. |
||||||||||||||||
|
Pro | zetaNetwork Instructor & Admin I Coded Most Of The Smexy Stuff You See ![]() PM Me Any Questions Need live support? Click here. | |||||||||||||||||
| Offline Profile | Quote ^ | ||||||||||||||||
| 1 user reading this topic (1 Guest and 0 Anonymous) | |||||||||||||||||
| « Previous Topic · Tutorials · Next Topic » |










bogging down your applications with loads of variables and other stuff you don't need. Using objects helps keep your neat and organized. Not to mention, you can have your own type of variables, well sorta. This tutorial will consist of 7 parts:
we'll be breaking it down line by line so don't worry, but first, let's go over what an object is: An object is basically a form of a class (see below). Each object is unique and holds it's own attributes and functions. Now let's get started cracking down on that block of code.
oop_part_1.zip (799 Bytes)
2:19 PM Jul 11