Results 1 to 10 of 10
  1. #1
    Join Date
    Jun 2003
    Posts
    232

    frameworks question

    most frameworks do not use abstract classes (at least not the ones i have seen) I have been writing a lot of libraries lately and everything has been abstract for reasons that should be obvious in the below example.

    *skim down near bottom of post to skim over the source before reading more*

    the advantages to this kind of abstraction are just obvious i think. The login is in no way attached to a login form it just sets the appropriate constants an such and the only thing you need to store in the session is the USER_ID and that is because you can get additional information through database joins.

    I know the advantage is obvious but I am just wondering if having to extend every class and write your own templates would annoy people to the point of not using it or if they would like the fact it was so abstract.

    I was coding all this stuff for myself but some of the stuff like my login class is just so useful that i wonder if it would be worth packaging and releasing or not.

    if anyone knows of any frameworks that are made up of nothing but abstract classes plz share the knowledge.

    The below is just all the files needed to extend this one class and have a functional login.

    Extended Login
    PHP Code:
    <?php
    final class Login extends Charles_Login {
        protected function 
    config() {
            
    $this->login       $_POST['login'];
            
    $this->logout      $_GET['logout'];
            
    $this->password  $_POST['password'];
            
    $this->username  $_POST['username'];
            
    $this->remember $_POST['remember'];
        }
       
        protected function 
    failure() {
            
    self::$error[] = 'wrong password';
            
    self::$error[] = 'user does not exist';
        }
       
        protected function 
    log_in() {
            if (
    $this->username === 'charles' && $this->password === 'password') {
                return 
    1//user_id
            
    }
        }
    }
    ?>
    login.tpl
    PHP Code:
    <?php
    $template 
    = new Template();

    if (
    USER_LOGGED) {
        
    $template->display('login/logged_in.tpl');
    } else {
        
    $template->display('login/logged_out.tpl');
    }
    ?>
    logged_in.tpl
    PHP Code:
    click <a href="<?=$_SERVER['PHP_SELF'] . '?logout=true'?>">here</a> to logout
    logged_out.tpl
    PHP Code:
    <?php if (USER_LOGIN_ERRORprint_r(Login::$error); ?>
    <form method="POST" action="<?=$_SERVER['PHP_SELF']?>">
    username:<br />
        <input type="text" name="username" /><br />
    password:<br />
        <input type="password" name="password" /><br />
    remember:<br />
        <input type="checkbox" name="remember" /><br />
        <input type="submit" name="login" />
    </form>

  2. #2
    Join Date
    Dec 2002
    Location
    United Kingdom
    Posts
    635
    having not used frameworks in php before i'd like to ask where your getting information about php from, so I cna have a read and understand what it is you're asking
    Saiweb ~ Ramblings of a sysadmin

    My Current Recomended WebHost: Nativespace



    "Acies Latin: a sharp edge or point; mental acuity, sharpness of vision"

    NOTE: Acies is currently beta, the "down" page will be displayed untill I have finished a release candidate

    3DBuzz has a reputation system please rate posts using the icon

  3. #3
    Join Date
    Jun 2003
    Posts
    232
    *before i start this message is mostly an explanation for why i no longer need an answer and what my original question was but if you have any input ill be glad to hear it*

    well the code i pasted was soo generic and error handling and pretty much everything worked in a way that is generic and does not require a touch of sql.

    That class or a variant of it will go in my coding library for all time. In short I have been writing a coding library for myself and have been trying to perfect it. I wanted stuff as generic as possible but the result was a lot of abstract classes.

    Abstract classes need to be extended in order to run and even if you didnt need to MY abstract classes (most of them) would not function with out being extended correctly. I thought that would make packaging the library for redistribution a problem.

    In some ways I still do I have to tweak my ideas a bit I think. But the question was would you find it annoying using someone elses library and having to extend it all the time.

    In most frameworks\libraries you can just do something like.
    PHP Code:
    $config = new Prefix_Config(args); 
    but with my config you would have to do

    PHP Code:
    final class Config extends Charles_Config() {
    public function 
    Configuration() {
    $this->config_file ''//default 
    $this->config_dir  ''//default (can change file but default dir is retained)
    }


    and a few other things. The reason I liked the idea of abstract so much was it completly removes the configuration from the program and places it inside an extended class example being
    PHP Code:
    final class Session extends Charles_Session() {
    protected function 
    config() {
    $conf Charles_Registry()->get('config_main');

    if (
    USER_REMEMBER) {
        
    $this->session_time $conf['session']['remember'];
    } else {
        
    $this->session_time $conf['$session']['forget'];
    }

    $this->session_name 'sess_id';

    I am typing this off memory as the code is on my laptop but the point is the config is separate to my Charles_Session and all i need to do from then is

    $session = new Session(); and its all configured and complete

    The question is really would the user having to extend the classes be enough to turn someone off it. I think the pay off is obvious but i would understand the frustration

    I decided to included default abstract versions many of which i hooked up to my config thing
    PHP Code:
    $charles = new Charles();

    Charles::DB();
    Charles::Session(); 
    i can access my stuff through public static and the return value is coming from a registry

    I hope you follow me so far.

    The abstract classes I think would hold value on their own but the question is would anyone want to use them?? would it be worth making

    1. an abstract version of the library\framework
    or
    2. a compiled library\framework that uses the abstracts

    or both.

    I do see benifit to both ways. If you think one way would be stupid or whatever just let me know.

    Also you wouldnt happen to know anything about licensing would you as I would like some licensing for my code that gives me control over my own code (i wouldnt exploit it but dont want to retain my rights unlike all that open source stuff where the client gets all the rights you have)
    Last edited by Nameless344; 11-07-2006 at 07:22 AM.

  4. #4
    Join Date
    Dec 2002
    Location
    United Kingdom
    Posts
    635
    Hmmm I've never had the need to abstract classes before, a personal preferance thing maybe?

    also I can't stand static calls class::function();

    but maybe that's just me.

    Thanks for explaining your framework.
    Saiweb ~ Ramblings of a sysadmin

    My Current Recomended WebHost: Nativespace



    "Acies Latin: a sharp edge or point; mental acuity, sharpness of vision"

    NOTE: Acies is currently beta, the "down" page will be displayed untill I have finished a release candidate

    3DBuzz has a reputation system please rate posts using the icon

  5. #5
    Join Date
    Jun 2003
    Posts
    232
    About personal preference I agree it may be very annoying for people to use unless its their own code. But if its their own code I think its a great tool.

    The difference I think is there are two options for very reusable code.

    1. dump a lot of objects\arrays and other stuff in arguments
    2. abstract it so the querys that get the data that gets dumped in the arguments are inside the class.

    There are many people (i used to do it as well when i was a lot newer) that code one massive function\object that gets all the data (instead of taking in an array of data they just query it WITHIN the function) that kind of thing cripples re usability I just think abstraction is a neater version of dumping objects in but It is harder to package for a third party I think.

    About static. You are aware of a singleton??

    well to access a singleton for me it was always

    $q = 'query';
    $r = DB::instance()->prepare($q);

    the static let me do a one liner and also let me only ever create one object (hence singleton)

    but compare


    $r = DB::instance()->prepare($q);
    $r = CharlesB()->prepare($q);

    I just think the second way is neater. I have a registry in my site that stores objects so I don't loose track of them. If i did not do it how I am now

    I would have to do

    Charles::Registery()->get('db')->prepare($q);
    or
    Charles_Registry()->get('db');

    and one last thing to end this.

    because I extend a lot of classes now I end up with names like

    Charles_Template();
    Charles_Template_Extended();

    this is one of the reasons I have gone the route of static (one of many) I used to try and avoid static like the plague but they help get around some design problems when needed.

    I with php's oop was a bit neater. I checked out a c# book today and the syntax... ooh i envy c#

  6. #6
    Join Date
    Dec 2002
    Location
    United Kingdom
    Posts
    635
    I personaly prefer "instancing" a class to an object. via a "new" call

    .. i.e.
    Code:
    $invoice = new invoice();
    
    ....
    
    
    $invoice->function($arg1, $arg2);
    Again I belive this is coming down to a personal coding style, I have had no real need to use the static singleton methodology in my php applications, I have used them in a few C++ ones, but that was so long ago I have forgotten as to why.

    I agree with the losing array in place of objects I use that for a lot of functions to I use a call as such to creat a new object

    Code:
    $this->objData	=	((object) NULL);
    The above is from one of my classes I use this call both in __construct() and __destruct() (PHP5)

    to first create the object for the use within the class (after declaring it as a private var at the top of the class)

    and then to reset it to a null object to conserve memory (as the classes are instanced if this is not done the set object data will remain in memory untill the script has finished executing).

    It's nice to see another perspctive on coding in php, that I have not come across before it always helps to diversify.

    In terms of extending classes in the "templates" I have that call classes etc to render html I have a function called init_classes();

    which inturn is called by the __construct();

    using this in conjuncion with the object method above I can use the following.

    Code:
    private $objClass;
    private $objData;
    
    public function __construct()
    {
          $this->init_classes();
          $this->objData	=	((object) NULL);
          $this->objClass	=	((object) NULL);
    }
    
    ...
    
    private function init_classes()
    {
          require_once("path/to/db.class.php");
          require_once("path/to/another.class.php");
          $this->objClass->DB = new db();
          $this->objClass->another = new another();
    }

    this can then inturn be used for entended classes instancing only the extended class give you access also to all functions in the "parent" class

    Code:
    private function init_classes()
    {
          require_once("path/to/db.class.php");
          require_once("path/to/db_extended.class.php");
          $this->objClass->DB = new db_extended();
    }
    using the above code, functions both from the parent and child classes can be accesed via once instanced object

    $this->objClass->DB

    resuability again is the key here, mine being from another perspective, thanks for sharing yours.

    (My code is also typed from memory so appologies for any typos that may be there :P )
    Last edited by Sion; 11-13-2006 at 07:08 AM.
    Saiweb ~ Ramblings of a sysadmin

    My Current Recomended WebHost: Nativespace



    "Acies Latin: a sharp edge or point; mental acuity, sharpness of vision"

    NOTE: Acies is currently beta, the "down" page will be displayed untill I have finished a release candidate

    3DBuzz has a reputation system please rate posts using the icon

  7. #7
    Join Date
    Mar 2006
    Location
    South Australia
    Posts
    4,521
    Well, am I right in saying that php does not have an interface type? In this case if I really got into OOP with php I would use abstract classes a lot. It enables you to take advantage of other design patterns such as Strategy, Facade, etc. For example, by using an abstract class/interface, you can write an entire cms library that is not dependant on any particular database system. You have an abstract Database class that has functions for manipulating the database, and the rest of your system uses these functions.

    Then, you can create classes for handling different database systems (mysql, postgresql etc, or even flat files/xml). You configure your system to use a particular class in a config file, and you have a system that is generic and uses design patterns properly to make it much easier to maintain, and much more flexible.

    I also like the idea of using a singleton pattern when getting a database connection. There shouldn't be a need to open up multiple connections, unless you are accessing multiple databases.

    Code reuse and ease of customising/extending is the go.

  8. #8
    Join Date
    Jun 2003
    Posts
    232
    I like parts of your approach I may of over looked it. The reason I avoided coding it that way was I did not like the idea of having to extend every single base class with the main class as php5 only allows for extending one class to my knowledge.

    I am going to try a new approach tomorrow that I think will be a winner that is using __CALL() to add things to the registry so all of that stuff is automated and requires less coding It should work just fine. That combined with a solid registry should be nice another advantage is I will use it to automate including the files for these classes meaning less code. The registry will be a singleton so I should be able to gain access to objects with out extending.

    oh to mr charisma do you know about the registry pattern?? i think you will like it . In short it allows you to create a singleton class with an array and that class has methods get() set() has() debug() and you can easily add things to the array inside the singleton. Allowing you to store multiple objects in the singleton. (meaning multiple database connections stored in singleton)

  9. #9
    Join Date
    Dec 2002
    Location
    United Kingdom
    Posts
    635
    As I always say the best approach is that which allows you to work most efficently.

    If it's a program / application to be done by you, then you're always best off following the coding style you are comfortable with.

    But as I also say it's always good to diversify.

    So I will be looking at your methods and seeing if they have any benifits to my currept apps

    :-)

    (I would add to your rep but keeps saying I need to "spread it arround")
    Saiweb ~ Ramblings of a sysadmin

    My Current Recomended WebHost: Nativespace



    "Acies Latin: a sharp edge or point; mental acuity, sharpness of vision"

    NOTE: Acies is currently beta, the "down" page will be displayed untill I have finished a release candidate

    3DBuzz has a reputation system please rate posts using the icon

  10. #10
    Join Date
    Jun 2003
    Posts
    232
    About the rep, I had no idea there even was a rep system but thanks i guess

    Last night you Gave me a million new ideas for how to use __GET __SET __ISSET __UNSET so thanks.

    But there are problems with your implementation but the way I was doing it before can be done so much better when using __SET or __CALL. The problems are it will be using a lot of methods and variables so I will need some sort of prefixing system or to choose rarely used names or just be anal about remembering what methods and vars are already taken. There are a few other things I found to be better with static variables.

    In my login script for example I would code

    self::$error[] = 'there was an error logging in';

    later in that code I would go and add it to Charles::$error['login'] through an error class that was pretty much an empty class with

    get
    set
    has
    debug

    methods.

    I dont know how I would do that with out static in a way that is flexible. It can probably be done I just dont know how.

    About what you say about everyone has their own coding style and you think I should do what suits me best. Like you I am looking for new ways of thinking about things to help learn. I have refactored a lot of the code in the project I showed you probably 5 times by now as I tried different ways to do it.

    Hopefully I will have it complete after 2 more tries. (I want it prefect so I can use it when i start getting work in web development) But I just think you learn more from the refactoring then the initial code anyway.

    Thanks for the input.

Posting Permissions

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