SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    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,

  2. #2
    SitePoint Addict
    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?

  3. #3
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    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.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,139
    Mentioned
    16 Post(s)
    Tagged
    3 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); 
    ActiveRecord
    PHP Code:
    $article = new Article(1);
    $article->title 'changed title';
    $article->save(); 
    Do some ORM research and these ideas should become a little more apparent.

  5. #5
    SitePoint Wizard
    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

    Similar with article, it is a real thing, it has a title, text, a date etc so:
    PHP Code:
    $article = new Article('title' => 'My Article''text' => 'awesome story text');
    $article->setPublished(true);
    $article->save(); 
    From there you can start to see what functions and variables your classes will need and can start fleshing out the code.

  6. #6
    SitePoint Enthusiast
    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.

  7. #7
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I think he meant he knew the Syntax by 'understand OOP'


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •