SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
-
May 6, 2009, 12:39 #1
- Join Date
- May 2008
- Posts
- 165
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
OOP - Understand, but can't put into Practice
So, I was sitting down having a discussion with some friends over OOP the other day, and how it works, what it's principals are, and how it relates to code, like a person.
However, after this, I took a look at the code I had written the evening before, which is in a class, with different functions. And it's WAY to narrow! I know putting it in a class is going to save me some time, but I may aswell have it procedural unless it is true OOP (after all that's what it was designed for).
When I say to narrow, I mean, I miss out the Object part, and each thing can only be used once!
So, after readiny many tutorials, I was wandering if anyone had some practical code / examples? For example, my admin class has a write_article function, which takes all the arguments it could require, and so is obviously only used once. Is this defeating the object (no pun intended!).
Thanks for listening,
-
May 7, 2009, 03:39 #2
- Join Date
- Nov 2005
- Location
- Germany
- Posts
- 235
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Oh boy. I can't speak about true OOP, and the topic is vast, but ok. Let's start with questions regarding your example:
1 What has writing an article have to do with the admin?
2 What properties/methods does an article/admin need?
-
May 7, 2009, 05:52 #3
- Join Date
- Sep 2006
- Location
- Nottingham, UK
- Posts
- 3,133
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Instead of an Admin->write_article class/method, the way to do it would be to have an Article class, which you use to add the data and save it. At the moment it's just sort of functions bunged into a class. Think in terms of objects rather than functions - 'Admin' is not an object (unless you are talking about an admin user), but 'article' is.
-
May 7, 2009, 06:27 #4
- Join Date
- Jul 2006
- Location
- Augusta, Georgia, United States
- Posts
- 4,194
- Mentioned
- 17 Post(s)
- Tagged
- 5 Thread(s)
Try creating a gateway or active record to manage the article data.
Gateway
PHP Code:$articleGateway = new ArticleGateway($db);
$article = $articleGateway->get(1);
$article->title = 'changed title';
$articleGateway->save($article);
PHP Code:$article = new Article(1);
$article->title = 'changed title';
$article->save();
-
May 12, 2009, 00:56 #5
- Join Date
- Nov 2005
- Posts
- 1,191
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You need to think of objects as actual entities. It sounds like your admin class might as well just be a file called admin_functions.php, with a set of admin related functions.
For example, as Stormrider mentioned, 'admin' isn't a real thing, it is a privilege. However 'user' is: it represents a person, has attributes like name, email etc, and methods like logout() so it is a candidate for an object:
PHP Code:$currentUser = new User('name', 'pass');
if($currentUser->isAdmin() {
// allow doing of admin stuff here
}
PHP Code:$article = new Article('title' => 'My Article', 'text' => 'awesome story text');
$article->setPublished(true);
$article->save();
-
May 14, 2009, 18:56 #6
- Join Date
- May 2007
- Posts
- 74
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
i do not understand why your said "OOP understand, but can't put into Practice", so i assume you are new to programming. or you though , you have understood "theory of OOP"?
just write a lot of code, oop or not do not matter.
what you need is to increase your software design/analysis skill.
buy some business systems analysis / design books.
start study data base modeling.
do not use design pattern, and do not read any pattern books, until your have
been a code monkey for 5 years.
there are a lot of PHP framework. zend, cake etc, all use OOP style.
-
May 14, 2009, 23:53 #7
Bookmarks