/    Sign up×
Community /Pin to ProfileBookmark

OOP conceptual questions

i’m going to develop an online tool similar to ‘blackboard’ if anyone’s used it; this site will contain grades and whatever else the teachers happen to post. behind the teachers will be several admin accounts that manage not only teachers, but students as well.

i’ve never created a site of this size. because i’d like for the code to be clean, efficiently done, and using the best programming concepts, i’ve decided to use OOP with this project. i am familiar with the syntax of it all (i’ve taken several C++ courses), instead i am unsure how i’d utilize OOP in the web development world.

i’ve read several tutorials and most used a shopping cart as an example. the first question that arose was:

1) what’s the variable scope of a class instance; does it automatically span from page to page or does it need to be in a session variable in order to do that?

now that some background is given in regards to the project, i was wondering how i should set up the classes, this is what i had in mind.

[code=php]
<?php
class Teacher {

function add_student() { /*add student*/t }
function delete_student() { /*delete student*/ }
function modify_student() { /*modify student*/ }
function post_documents() { /*postdocuments*/ }
function edit_gradebook() { /*edit gradebook*/ }
}

class Admin extends Teacher
{
function add_teacher() { /*add teacher*/ }
function remove_teacher() { /*remove teacher*/ } ;
function add_admin() { /*add admin*/ } ;
function remove_admin() { /*remove admin*/ } ;
}

class Student
{
function show_homework() { /*show homework*/ }
function show_grades() { /*show grades*/ }
function change_info() { /*change pw or email*/ }
}
?>
[/code]

2) how much of the php code should be on the page using the class.php include and how much code should be in the class.php itself (the code pasted above)?

3) do classes generally include functions that echo html fields and content?

any tips regarding this would be highly appreciated. thanks.

to post a comment
PHP

18 Comments(s)

Copy linkTweet thisAlerts:
@bokehMar 16.2008 — [LIST=1]
  • [*]Nothing is persistent in PHP, not even bacteria. You can use sessions, files or DB for storage. You can store objects in sessions but you must load the relevant classes (that created the objects) before calling [I]session_start[/I].

  • [*]Your choice.

  • [*]Read up on MVC. Never use a class to store data. If your class is going to send output, import that output. This is common sense really because if your class has output hard coded into it it becomes tied to the application.

  • [/LIST]
    Copy linkTweet thisAlerts:
    @ixxalnxxiauthorMar 17.2008 — MVC is the seperation between logic and presentation, right? is it necessary to use libraries and frameworks (mojave, cakephp)? do you use any frameworks?
    Copy linkTweet thisAlerts:
    @NogDogMar 17.2008 — You do not have to use a 3rd-party framework. Some advantages of using one are that much of the groundwork has been done for you already, and hopefully it has been well designed and well tested, so that you can have a high degree of confidence that it will be reliable and effective. Some disadvantages are that you have to learn how to implement the framework and perhaps modify your ways of working and designing applications to mesh with the way that framework is designed. (This impact is reduced as the size of the project increases and/or the number of projects you use it with, making that learning time a smaller percentage of your total implementation time.)

    For a project with a handful of input/output pages and only a few database tables, implementing a 3rd-party, full-fledged MVC framework may be overkill in terms of that time to ramp up to becoming fluent with it. On the other hand, if your schedule permits it, it might be a good opportunity to find one you like and learn how to use it
    Copy linkTweet thisAlerts:
    @bokehMar 17.2008 — Although often used together, OOP, MVC and frameworks are three individual things that can be implemented separately.

    As for using third-party code (frameworks or otherwise) your not going to learn a lot except about someone else's code and style. And even if you read every line of the code and try to reverse engineer the thought process behind the way it was written you probably never will. Just imagine a route from A to B; one person takes a longer route but arrives first. Did he take that route because he knew it was quicker, or did he take the longer route because it was the only one he knew, and just happened to arrive first by luck? What unseen things did he avoid on the journey?

    The bottom line is if you use someone else's code it may speed up the coding process (especially if you are a novice) but you won't be learning much and will probably end up with a lot of unnecessary code that you will never use.
    Copy linkTweet thisAlerts:
    @Sid3335Mar 17.2008 — I used blackboard when I was at University. Never liked it.

    Hope you improve upon it ?
    Copy linkTweet thisAlerts:
    @ZnupiMar 17.2008 — So what are you saying, bokeh, that we should create our own frameworks? Why reinvent the wheel? Why not use a framework developed by a team of experienced programmers who spent a lot of time developing and improving it? Why not use a framework many other people are able to use, too? Especially when working in a team, and work is divided between the coder(s) and the designer(s) which need to be able to communicate in a successful manner.

    I never used a framework, but I've read some things about them. I've been wanting to learn smarty since it was recommended to me by my older (and more experienced) brother, but I didn't get the time.

    I don't see why this has evolved in a discussion about frameworks. [B]ixxalnxxi[/B], the important thing is: you can not store data in classes! It gets deleted when the script finishes execution, just like all the other variables. You will, most probably, have to use a database for that. Of course, you can use those classes and functions to work with the data in the database (add/modify/delete rows, etc).

    Also, don't forget that sessions get destroyed when the user closes his browser, so that's not a good place to store long-term data either.
    Copy linkTweet thisAlerts:
    @ixxalnxxiauthorMar 17.2008 — i am unaware of where i mentioned of storing data in a class, but i do realize that data can not be stored in a class (that's what mysql is for ^_^)
    Copy linkTweet thisAlerts:
    @bokehMar 17.2008 — So what are you saying, bokeh, that we should create our own frameworks? Why reinvent the wheel?[/QUOTE]I didn't say don't use frameworks, I said you're not going to learn anything. Also you are going to need to shoehorn your app to fit the framework.

    I never used a framework, but I've read some things about them. I've been wanting to learn smarty since it was recommended to me by my older (and more experienced) brother, but I didn't get the time.[/QUOTE]Smarty is not a framework, it is a template system. The idea is to put an extra layer between the code and the people using it. It is for companies that have few real programmers and the rest of their staff are lower grade employees. The template system keeps those less employees from breaking the code.

    you can not store data in classes![/QUOTE]Of course you can. If your class contains a line that says $var = 1; that is stored data. And it's persistent.
    Copy linkTweet thisAlerts:
    @bokehMar 17.2008 — i am unaware of where i mentioned of storing data in a class[/QUOTE]Well kind of read your point 3 that way so it's my fault.
    Copy linkTweet thisAlerts:
    @ixxalnxxiauthorMar 18.2008 — Well kind of read your point 3 that way so it's my fault.[/QUOTE]

    it's all good -- you and the rest of the contributors have been really helpful. i appreciate it.
    Copy linkTweet thisAlerts:
    @ixxalnxxiauthorMar 18.2008 — question, since MVC emphasizes a separation between presentation and logic, would it be a bad idea to use a class for the footer and header?

    (i figure that instead of using normal require/include i can use the functions for speed reasons)

    also, in my initial class blueprint i have several different functions that in essence to the same thing:
    [code=php]
    function remove_teacher()
    function delete_student()
    function remove_admin()
    [/code]


    would it be better to create a single function for all of them?
    [code=php]
    function removeElement($id, $table)
    {
    $query = "DELETE FROM $table WHERE id = $id LIMIT 1";
    if(mysql_query($query))
    return "Successfully Deleted";
    else
    return "Error In Deletion";
    }
    [/code]
    Copy linkTweet thisAlerts:
    @bokehMar 18.2008 — question, since MVC emphasizes a separation between presentation and logic, would it be a bad idea to use a class for the footer and header?[/QUOTE]Of course you can use a class to do that... but the class/method/function is a template. Such a function most probably be an orphan though.

    Your header template function would have a standard header or headers hard coded into it and would take the changeable parts (title, etc) through variables retrieved from your data source (DB, files, etc).
    Copy linkTweet thisAlerts:
    @ZnupiMar 18.2008 — I didn't say don't use frameworks, I said you're not going to learn anything. Also you are going to need to shoehorn your app to fit the framework.[/QUOTE]
    So you are still saying that we should create our own frameworks? Why do something so time consuming when someone has already done it, maybe even better?
    Smarty is not a framework, it is a template system. The idea is to put an extra layer between the code and the people using it. It is for companies that have few real programmers and the rest of their staff are lower grade employees. The template system keeps those less employees from breaking the code.[/QUOTE]
    Although Smarty is known as a "Template Engine", it would be more accurately described as a "Template/Presentation Framework." That is, it provides the programmer and template designer with a wealth of tools to automate tasks commonly dealt with at the presentation layer of an application. I stress the word Framework because Smarty is not a simple tag-replacing template engine.[/quote]
    And I disagree with your opinion on the users of Smarty being "lower grade employees". In the company in which my brother works, the "real programmers" do both the coding and the smarty templates, the designer only makes the HTML&CSS templates (&flash, etc). So it's not for the "lower grade employees", it's for all of them, it keeps code nicely separated.
    Of course you can. If your class contains a line that says $var = 1; that is stored data. And it's persistent.[/QUOTE]
    Maybe you haven't read my whole post?
    ...It gets deleted when the script finishes execution, just like all the other variables.[/quote]
    I really think the OP understood that I was referring to data that needs to be there after the script finishes execution.
    Copy linkTweet thisAlerts:
    @ss1289Mar 18.2008 — 
    also, in my initial class blueprint i have several different functions that in essence to the same thing:
    [code=php]
    function remove_teacher()
    function delete_student()
    function remove_admin()
    [/code]


    would it be better to create a single function for all of them?
    [code=php]
    function removeElement($id, $table)
    {
    $query = "DELETE FROM $table WHERE id = $id LIMIT 1";
    if(mysql_query($query))
    return "Successfully Deleted";
    else
    return "Error In Deletion";
    }
    [/code]
    [/QUOTE]


    That's exactly what you want to do.

    One thing that I do about deleting from a database though is that I create a "state" column in my database and set it to 1 if the row (or user, in your case) is active or set it to 0 if not active (or deleted). That way you don't actually permanently deleted it and can easily get that info back if you need to for some reason. Then when you check for active users, always check that column for a 1.

    I actually use this practice in almost every table. What I actually have is a table that has several rows each of a different state (like 1=active, 2=not active, 3=pending, 4=prospective). I then associate it with almost every table which contains a column called "state" that contains one of those numbers.
    Copy linkTweet thisAlerts:
    @bokehMar 18.2008 — Are you just looking for an argument? So you are still saying that we should create our own frameworks?[/QUOTE]I said MVC, it was the OP that brought up the question of frameworks. Whether or not a framework be necessary or desired, and, whether or not that framework be home grown or written by a team of acclaimed masters should be dealt with on a case by case basis. By the way only a cowboy coder would ask such a question anyway since all others have to comply with departmental methodology.

    I disagree with your opinion on the users of Smarty being "lower grade employees".[/QUOTE]From smarty dot net: "[I]Templates do not contain PHP code. Therefore, a template designer is not unleashed with the full power of PHP, but only the subset of functionality made available to them from the programmer[/I]". To me that implies Smarty is just an unnecessary layer that keeps the little boys away from the men's work.
    Copy linkTweet thisAlerts:
    @ZnupiMar 18.2008 — Are you just looking for an argument?[/quote]
    No, it's just that I like contradictory discussions ? I don't consider them offensive in any way, and please excuse me if I'm being offensive. I don't mean to.
    From smarty dot net: "[I]Templates do not contain PHP code. Therefore, a template designer is not unleashed with the full power of PHP, but only the subset of functionality made available to them from the programmer[/I]".[/QUOTE]
    To me, that seems perfectly normal: you don't need the full power of PHP when designing the template. For example: you don't connect to mysql and run queries inside a template, do you? That's the whole point of having a framework, isn't it?
    Copy linkTweet thisAlerts:
    @Jeff_MottMar 18.2008 — It is for companies that have few real programmers and the rest of their staff are lower grade employees. The template system keeps those less employees from breaking the code.... Smarty is just an unnecessary layer that keeps the little boys away from the men's work.[/quote]I agree with Znupi here. It doesn't matter how smart the front-end developers are. A templating system is just good software design.
    Copy linkTweet thisAlerts:
    @ixxalnxxiauthorMar 19.2008 — while handling forms, would it be reasonable to do the function call removeElement that has 2 returns, "success" or "fail", in the middle of the html document? or does that blur the logic/presentation ?
    ×

    Success!

    Help @ixxalnxxi spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.28,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...