OOP
Moderator: General Moderators
OOP
Ok, so i think understand some of the very basics of OOP but I'm still have no clue how to really use it.
I can make an object, say a person - and give them a name and age and other things, but then I'm not really sure what to do. There doesn't seem much use if it is non dynamic. I guess part of my problem is that I'm not really sure how use it dynamically. I must be thinking about them in the wrong way. For example, if I have a page, where a user can do stuff, such as maybe: write text, rate things, sorting lists, putting items into shopping baskets, am I meant to have some procedural code at the start? Such as, if user presses a certain button, start this particular object; and if another button do another object.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Kind of. What you're talking about is controller logic. You're right in your assumption that without code to actually work with objects there's nothing much going to happen. However, the procedural code used to launch that entire sequence of events can be a single line 
Objects can pass messages to each other. Once you get familiar with a few design patterns it will probably make more sense 
Code: Select all
AppContext::getInstance()->getController()->dispatch();- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: OOP
A main difference between Procedural and OOP conceptually is that Procedural starts with functions and passes data to them, whereas OOP starts with data and calls functions that are only allowed to operate on that data. You can think of a class like a function library that shares a set of global vars.Shears wrote:Ok, so i think understand some of the very basics of OOP but I'm still have no clue how to really use it.I can make an object, say a person - and give them a name and age and other things, but then I'm not really sure what to do. There doesn't seem much use if it is non dynamic. I guess part of my problem is that I'm not really sure how use it dynamically. I must be thinking about them in the wrong way. For example, if I have a page, where a user can do stuff, such as maybe: write text, rate things, sorting lists, putting items into shopping baskets, am I meant to have some procedural code at the start? Such as, if user presses a certain button, start this particular object; and if another button do another object.
Hopefully you start to see the benefits, such as having data shared by a group of function but protected from all other functions, or that you could use the same common function name in two different classes and not have a name clash.
Beyond that there are a bunch of conceptually interesting and practical things that you can do with objects that can improve reuse-ability and maintainability. But at its simplest, a disciplined Procedural programmer can achieve the modularity that OOP provides.
So start by thinking of the data first. Define a data structure and how it is used, then add code to operate on that data.
(#10850)
Re: OOP
Thanks for both your responses.
At the moment, if i want to write a class, I tend to end up thinking of it procedurally, and just divide the code up into sections (functions), and let the control just flow linearly through the functions... I guess I'm not doing it right
What do you mean "Procedural starts with functions"?
Does that make sense?
Thank you
Shears
At the moment, if i want to write a class, I tend to end up thinking of it procedurally, and just divide the code up into sections (functions), and let the control just flow linearly through the functions... I guess I'm not doing it right
I'm not sure I really understand this.arborint wrote:A main difference between Procedural and OOP conceptually is that Procedural starts with functions and passes data to them, whereas OOP starts with data and calls functions that are only allowed to operate on that data. You can think of a class like a function library that shares a set of global vars.
It's frustrating because I appreciate that there are many benefits - if only i could understand them.Hopefully you start to see the benefits, such as having data shared by a group of function but protected from all other functions, or that you could use the same common function name in two different classes and not have a name clash.
I often here about this - and stuff like OOP allowing modular coding. Maybe you have a core code, and you can add stuff without changing the actual core code. I cant even begin to imagine how this might be done. How would the core code know that something extra exists without it being modified?Beyond that there are a bunch of conceptually interesting and practical things that you can do with objects that can improve reuse-ability and maintainability.
Thank you
Shears
I'm just a little curious why OOP questions tend (not always) to get ignored?
I cant believe it's because the question is too simple. Maybe it's just because people get bored with trying to teach different n00b about the same OOP.
But then again whatever is in this coding forum it must be like that to some people...
Maybe I am not asking a specific question? I will ask some then...
Like I said in my previous post. I could just write something procedurally - then divide it up into a few sections, and stick the sections in functions which control flows linearly between. Is this really OOP?
What is the point in all these get and set functions i see? Why not just use $this->variable directly in your code instead of having a special function that does nothing but return a variable..?
Thank you
Shears.
PS. From what I've seen for OOP, essentially, either you understand it well and frequently use it, or you just steer clear. There doesn't seem to be much middle ground. There are quite alot of OOP tutorials around that I've found but they all seem to cover the same very basic stuff and do not go beyond. Or am I wrong? Maybe i am not searching with the right words
Like I said in my previous post. I could just write something procedurally - then divide it up into a few sections, and stick the sections in functions which control flows linearly between. Is this really OOP?
What is the point in all these get and set functions i see? Why not just use $this->variable directly in your code instead of having a special function that does nothing but return a variable..?
Thank you
Shears.
PS. From what I've seen for OOP, essentially, either you understand it well and frequently use it, or you just steer clear. There doesn't seem to be much middle ground. There are quite alot of OOP tutorials around that I've found but they all seem to cover the same very basic stuff and do not go beyond. Or am I wrong? Maybe i am not searching with the right words
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
No no, you don't steer clear. All of us, at some point, were completely baffled by OOP.
And if people didn't answer, it's not because they don't want to explain it... It's just not easy to explain.
Think of it as a sentence. A class is a bunch of adjectives and verbs, and an object is a noun. A noun is just a noun until it has a description and a purpose.
The variables of the class are the adjectives.
The functions of the class are the verbs.
So, lets say we have a class called.. builder. His adjectives are... strength, work ethic, and intelligence.
The builder class has the verbs: plan, build, and take a break.
So, here's our class:
Then, we'd make a noun of type "Builder."
And you could make $Joe do stuff.
The trick about classes is that everything that you make $Joe do, $Joe does based on HIS adjectives. So, if Joe is weak, he'd get less work done, but if he is intelligent, he'd have higher quality planning.
You'd create the verbs (functions) to reflect the way that everything should be done based on the adjectives (member variables).
The purpose of classes is to give objects tasks, give them data to do the tasks with, and have them communicate the results to either the application or to other objects.
Think of it as a business. The separation of tasks gets work done faster, more efficiently, and much more neatly.
And if people didn't answer, it's not because they don't want to explain it... It's just not easy to explain.
Think of it as a sentence. A class is a bunch of adjectives and verbs, and an object is a noun. A noun is just a noun until it has a description and a purpose.
The variables of the class are the adjectives.
The functions of the class are the verbs.
So, lets say we have a class called.. builder. His adjectives are... strength, work ethic, and intelligence.
The builder class has the verbs: plan, build, and take a break.
So, here's our class:
Code: Select all
class Builder
{
private $strength, $workEthic, $intelligence;
public function __construct(); // Constructor
public function Plan();
public function Build();
public function TakeBreak();
}Code: Select all
$Joe = new Builder();Code: Select all
$Joe->Plan();
$Joe->Build();
$Joe->TakeBreak();You'd create the verbs (functions) to reflect the way that everything should be done based on the adjectives (member variables).
The purpose of classes is to give objects tasks, give them data to do the tasks with, and have them communicate the results to either the application or to other objects.
Think of it as a business. The separation of tasks gets work done faster, more efficiently, and much more neatly.
Re: OOP
I think it's easier to think about this with a concrete example.. .'Pagination' is a very common topic..Shears wrote:Ok, so i think understand some of the very basics of OOP but I'm still have no clue how to really use it.
If you're not familiar with the topic, i'd recommend that you read up on:
viewtopic.php?t=43777&postdays=0&postor ... n&start=60
(In case you don't find code in the thread anymore, i kept a copy of the code i wrote)
As the code shows it allows you quickly implement a different datasource or renderer without having to modify the other parts.. The separation of concerns is what makes (imho) OOP this powerful.
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Thanks. ^_^ Came out of nowhere.Kieran Huggins wrote:wow SD, what a good analogy!
I'm thinking of making it into an article. :-p
Lol. It's got plenty of views. People see OOP and go, "Ooh! I wanna see what they say in this one." At least, that's what I did. :-pKieran Huggins wrote:I tend to agree, people ignore the thread partly because it's difficult to answer. Also, your title is a bit vague... I usually skip the vague titles. This was my first time looking at it, not even sure why I did!
Re: OOP
This is something which I can't seem to grasp either, how can you extend for example a class with some additional functionality without previously having prepared "hooks" for those functions in the base class?Shears wrote:I often here about this - and stuff like OOP allowing modular coding. Maybe you have a core code, and you can add stuff without changing the actual core code. I cant even begin to imagine how this might be done. How would the core code know that something extra exists without it being modified?Beyond that there are a bunch of conceptually interesting and practical things that you can do with objects that can improve reuse-ability and maintainability.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: OOP
Well, when I hear "extends," I think inheritance, which is probably what you're referring too.vigge89 wrote:This is something which I can't seem to grasp either, how can you extend for example a class with some additional functionality without previously having prepared "hooks" for those functions in the base class?Shears wrote:I often here about this - and stuff like OOP allowing modular coding. Maybe you have a core code, and you can add stuff without changing the actual core code. I cant even begin to imagine how this might be done. How would the core code know that something extra exists without it being modified?Beyond that there are a bunch of conceptually interesting and practical things that you can do with objects that can improve reuse-ability and maintainability.
Like, having a Person class with basic person attributes and functions, and then having a more specific Worker class that has all of the Person attributes and functions, as well as it's own.
This allows multiple classes to share functionality, but still have specific purposes. It also makes the base Person class infinitely re-usable rather than recreating a new class each time you wanted something similar to Person, but not quite.
So. How might a fully OOP web page look? For example, if this page in a very very basic form on these forums were done in OOP, might it be something like...?
Part of my problem is that I am not too sure what I am trying to build towards or what I am aiming for... It's hard to see how a static OOP page (does that make sense?), which is all I am really able to do at the moment can become anything more
I have never really seen any class that is longer than about 50 lines
Where do I find out more advanced things than a "build a rectangle class"? Are there some golden websites around? Or is it just various books? I am not really able to ask good questions here as I dont have enough of an understanding and so I dont know what questions to ask
Thank you
Shears
Code: Select all
<?php
require 'user_authentication.php';
require 'forum_functions.php';
$user = new authentication;
$user->is_user_logged_in($user_id, $time_last_action);
$user_menu = new menu;
$user_menu->type_of_menu('top');
$thread = new thread;
$thread->thread_info($thread_id, $user_id, $post_per_page);
?>Where do I find out more advanced things than a "build a rectangle class"? Are there some golden websites around? Or is it just various books? I am not really able to ask good questions here as I dont have enough of an understanding and so I dont know what questions to ask
Thank you
Shears
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Well, there are a lot of OOP tutorials out there, but when it comes down to it, the implementation is up to you. I just redesign my website (only the back-end) and it's set up as only a config file, an index, a .htaccess, and a huge folder of classes. The index looks like this:
And all of the pages are handled through that and a URL parser. Every URL is redirected to index.php and I break them into pieces and determine from those pieces what do with them. Of course, you don't need to do that as it's not exactly "beginner" territory.
If you'd like an example, here are all of the folders in my classes folder (each folder holds a class and it's related abstracts, children, interfaces, and such). Keep in mind, this is an OO blog I made.
Code: Select all
<?php
include_once('include.php');
$pPageDisplay->ShowPage();
?>If you'd like an example, here are all of the folders in my classes folder (each folder holds a class and it's related abstracts, children, interfaces, and such). Keep in mind, this is an OO blog I made.
- category - Define categories and subcategories for listing posts from the database
- comment - Define comments and comment lists for posts
- display - All of the data passes through these classes (a lot of them based on one abstract) to be formatted into my layout and displayed
- geshi - GEneric Syntax HIlighter (third-party class)
- log - Handles error logging and statistical logging
- pagination - A few different pagination classes depending on the style of pagination I use for each list
- post - Defines posts
- registry - A registry pattern class that holds all data that should be "globally" available
- rss - Handles the creation and updating of RSS feeds
- session - Handles sessions
- sha256 - SHA256 hashing class (third-party class made by feyd)
- sql - A few classes dealing with databasing
- swift - Swiftmailer (third-party class by d11wtq)
- urlparser - Handles parsing of URLs
- user - Handles logging in and user validation