SitePoint Sponsor

User Tag List

Page 1 of 4 1234 LastLast
Results 1 to 25 of 87

Hybrid View

  1. #1
    SitePoint Zealot Selatos's Avatar
    Join Date
    Aug 2002
    Location
    USA
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    When to use and when not to use OOP

    I've been working with PHP for a while, and OOP in PHP for a slightly shorter while, and often the question "do i need a class for this?" has been raised. How do I know if i need a class? Generally, I follow these guidelines:
    A) Will i use this code over and over again?
    B) Is this class something that I can easily modify to make it applicable in similar situations?
    C) Is what it accomplishes important enough for me to make a class for it, instead of just coding it every time?

    A little while ago(about 10 minutes), my friend proposed a new site design for a site he is working on, and said he would do everything in OOP, inlcuding the links. I told him it was a waste of time to make a class for the links, and certain other things, and also a waste of resources. For example, a simple link class might look something like this:
    PHP Code:
    <?php
    class Link {
    var 
    $url;
    var 
    $title;
    var 
    $titletag;
    function 
    Link($url$titletag$title) {
    $this->url $url;
    $this->titletag $titletag;
    $this->title $title;
    }
    function 
    getTag() {
    return 
    '<a href="'.$this->url.'" title="'.$this->titletag.'">'.$title.'</a>';
    }
    }
    //some links
    $linkA = new Link("index.html""Somewhere""Somewhere");
    $linkB = new Link("main.html""Someplace""Someplace");
    echo 
    $linkA->getTag();
    echo 
    "<br>";
    echo 
    $linkB->getTag();
    My friend believes that a class similar to this one(i dont know his exact specifics) would be more advantageous than something more like this(straight HTML output without the overhead of class instances):
    HTML Code:
    <a href="index.html" title="Somewhere">Somewhere</a>
    <a href="main.html" title="Someplace">Someplace</a>
    Think about it mathematically, one byte per letter, averaging 10 letters per string x 3 strings, we have 30 bytes per link. Not much true, but thats 30 bytes that dont need to be wasted, and lets say there are 10 links on a page. thats 300 bytes per page. Now, there are 10 people accessing the site at the same time, 3 KB. The site grows, 10k hits per day. 300 x 10k bytes of memory wasted to process links that could have simply been sent to the browser without wasting resources.

    This made me wonder, where does one draw the line for what should be an Object and what shouldn't? Any thoughts?

  2. #2
    Romans 12:2 codyrockx's Avatar
    Join Date
    Jul 2003
    Location
    Newberg, Oregon
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oop should be used for when you need to reuse the code effectively, to speed your website, and to enable your website to be dynamic

    1. oop - object oriented programming, html is the complete opposite, it's static and is pointless to make a class to output it
    2. effectively - creating a class to output html is static, how you have it setup it makes it not at all dynamic and very unstable, you only allow certain tags and are requiring an entirely different language to ineffectively parse something that can be parsed easy
    3. speed - creating a class to output html is nonsense, one, it slows down the code, it takes up twice as much resources having to run it through the html parser and the php parser to acheive the same output
    4. dynamic - oop is for a website so that it may be dynamic, dynamic as in reusing the code, and later down in the script being able to modify the object, play with it, and output it to your needs, html is static
    Codyrobert.com - Designer and Developer

  3. #3
    SitePoint Guru
    Join Date
    Dec 2003
    Location
    oz
    Posts
    819
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OOP isnt the issue here.
    Keep your dynamic and static stuff seperate.
    Dynamic links will use a DAO class to get the info and a templating engine to display, where as static stuff is written straight onto the page

  4. #4
    SitePoint Zealot Selatos's Avatar
    Join Date
    Aug 2002
    Location
    USA
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you're saying that I was right in telling him that its a waste of a class?

    He also told me that it can be used to dynamically make links, but so can a good SQL query, a loop, and some variables....without the overhead of a class either.

  5. #5
    Romans 12:2 codyrockx's Avatar
    Join Date
    Jul 2003
    Location
    Newberg, Oregon
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    key phrase for this situation:
    why use two parsers (php and html) when you only need one (html)?
    Codyrobert.com - Designer and Developer

  6. #6
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I actually think that a link builder class is a good idea. I'd rather have my code keeping track of things (links could be in the database or a config file for example) than an html file. Especially if you're passing arguments in the query string.

    If you have a look at the Url class in the Eclipse library you might get a better idea for what a 'link' class could do. I use it and there have been situations (paging result sets for example) that it has *really* helped out.

    There are other advantages to OOP in that you can seperate related code avoiding the endless procedural script, and it's great for avoiding variable name collisions. I don't know how you can even *compare* html and oop? That's more extreme than comparing oranges and apples. And if you ask me, I'd rather see/work with some nicely designed OOP code than speedy procedural scripts.

    oop should be used for when you need to reuse the code effectively, to speed your website, and to enable your website to be dynamic
    I don't think that OOP has anything to do with dynamic, or a website being speedy. Procedural code can be just as dynamic? There are good classes (including html outputing classes) and there are bad classes. It's all about how you want to solve your problem.

    Matt

  7. #7
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this class something that I can easily modify to make it applicable in similar situations?
    Am I the only one who thinks this is wrong ? What I mean is that once you have a class that is debugged, and working as you want it, and later you need more functionality then you should extend a child class to it instead ?

    In my view, I'd only refactor it later if I had to do so to make it (interface) cleaner in regards to how a child class uses (base) the class

    On the other hand, if there is a lot of new functionality to be created, you'd be better of just creating a new class altogether.

    As to when to use OOP I'm biased as I'd proberly recommend you use it anyway by default

  8. #8
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Widow Maker
    Am I the only one who thinks this is wrong ? What I mean is that once you have a class that is debugged, and working as you want it, and later you need more functionality then you should extend a child class to it instead ?
    Inheritance is often overused in OOP, particularly in situations where the "Is A" rule is violated. Ofter a decorator is a better way to implement different behaviors for an object.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  9. #9
    SitePoint Zealot sleepeasy's Avatar
    Join Date
    Sep 2003
    Location
    Bristol, UK
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    Inheritance is often overused in OOP, particularly in situations where the "Is A" rule is violated. Ofter a decorator is a better way to implement different behaviors for an object.
    I definately agree with that - I find inheritance tends to add confusion more than anything else, and the degree of confusion increases drastically as the inheritance tree grows.

    Strangely I find inheritance to be most useful not when it's used in (what I call) the traditional way - but in cases such as the Generation Gap pattern.

    Interfaces IMHO are much more useful. You can test whether you can ask an object to do something without forcing the object to be of a certain type and possibly inherit functionality it may not require. Granted this will depend on how you organize your class structure.

    Also, because an object can implement more than 1 interface you can avoid making complex class hierarchies - just imagine the inheritance nightmare that would be invloved in representing an object that implements Persist_IDataStoreStorable, App_IService, IInspectable. Where would you start?

    Well, sorry for going off-topic.
    Last edited by sleepeasy; Jul 22, 2004 at 05:44.
    Always open to question or ridicule

  10. #10
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What ? A painter & decorator ?

    I'm aware of this pattern, didn't realise that there'd be a violation though

  11. #11
    SitePoint Evangelist Andrewaclt's Avatar
    Join Date
    Dec 2003
    Location
    Raleigh, NC
    Posts
    535
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function getTag() {
    return 
    '<a href="'.$this->url.'" title="'.$this->titletag.'">'.$title.'</a>';

    Instead of the above function you recommend either:

    • Modifying the function that returns all the values in an array intead of some html?
    • Creating multiple functions to return the values such as getUrl(), getTitle() or getTitleTag()?
    • Sticking with html and not using an object?

  12. #12
    SitePoint Zealot Selatos's Avatar
    Join Date
    Aug 2002
    Location
    USA
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah I would reccomend sticking with HTML, because I don't see any advantages of using a class other than the code looking nice and pretty.

    I like inheritance, although I don't use it very often in PHP, because it allows me to code a simple class that does something and then add functionality later(these classes I don't modify), but sometimes I prefer to just alter the base class if I only want to add a function or change the behavior of a function to suit a slightly different purpose.

    Anyways this is about when to use OOP and when not to, if it relates to that it isn't really off-topic anyways.

  13. #13
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your comments SweatJs and SleapEasy

    Persist_IDataStoreStorable, App_IService, IInspectable.
    Is this ideal for the Visitor Pattern or the Decorator Pattern. Don't both do just about the same thing ?

    Creating multiple functions to return the values such as getUrl(), getTitle() or getTitleTag()
    My personal favourite

  14. #14
    SitePoint Enthusiast NativeMind's Avatar
    Join Date
    Aug 2003
    Location
    USA
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Widow Maker
    Is this ideal for the Visitor Pattern or the Decorator Pattern. Don't both do just about the same thing ?
    A decorator is used as an alternative for inheritance to avoid a large amount of sub-classes, or when you don't want to affect an existing object structure

    PHP Code:
    class Box {
       var 
    $height;
       var 
    $width;

       function 
    getArea() { return $this->height $this->width; }

    }

    class 
    BorderedBox {
        var 
    $box;
        var 
    $border;
         
        function 
    BorderedBox() { $this->box = new Box(); }

        function 
    getArea() {  
           
    $this->box->height += border;
           
    $this->box->width += border;
           return 
    $this->box->getArea();
         }

    The visitor pattern is used across many classes that may have varying interfaces and you don't want to pollute or complicate the existing class structure with a lot of little changes. Also if you have to share an object structure with many different applications, a visitor can be used for just the operations needed by the application.

    PHP Code:

    class BoxVisitor {
         function 
    Visit($box) { 
              echo 
    "A box has:";
              echo 
    "  height: ".$box->height;
              echo 
    "  width: " $box->width;
         }
    }

    class 
    Box {
        var 
    $height;
        var 
    $width;
        function  
    Accept($visitor) {
             
    $visitor->Visit($this);
         }

    Ok, as you see that these are very similar - but a decorator is really like "decorating", you wrap some existing class up with something special. The result is that the decorator can be more flexible than inheritance alone. You can also simplify a class hierarchy by putting those less-frequently used operations into a decorator. Unfortunately, this means that the decorator and the component it decorates are not identical and can not be enforced with inheritance style rules. Also, you wind up with a lot more little decorator objects which are easy to use for those that know them, but harder for people new to the code, or sometimes more complex to debug.

    The visitor pattern is again - meant for an entire object structure, and adding an operation is as simple as adding a new visitor. So, all your components must support the visiting interface. This lets your visitor classes separate the algorithms of the data structures in anyway you want because they can go in and visit anything. Unfortunately, this means that as soon as you add something to your object structure, you have to make sure it can be visitted and update all your visitors to use that information (if necessary). Visitors are good if you want to change algorithms and leave data structures alone. Visitors may also accumulate state as you can have a visitor visit more than one object. Unfortunately, you can also break encapsulation by giving too much information to visitors.

  15. #15
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    [QUOTE=Selatos]For example, a simple link class might look something like this:
    PHP Code:
     <?php
     
    class Link {
     var 
    $url;
     var 
    $title;
     var 
    $titletag;
     function 
    Link($url$titletag$title) {
     
    $this->url $url;
     
    $this->titletag $titletag;
     
    $this->title $title;
     }
     function 
    getTag() {
     return 
    '<a href="'.$this->url.'" title="'.$this->titletag.'">'.$title.'</a>';
     }
     }
     
    //some links
     
    $linkA = new Link("index.html""Somewhere""Somewhere");
     
    $linkB = new Link("main.html""Someplace""Someplace");
     echo 
    $linkA->getTag();
     echo 
    "<br>";
     echo 
    $linkB->getTag();

    I think this could be useful if you wanted to be able to manage groups of links, like in a CMS-type app.
    Of course the purists won't like you creating HTML in this class, they might be right, but if you're gonna
    add getUrl(), getTitle() and getTitleTag() accessor methods, then the object you have is still pretty much
    specific to an HTML link anyway isn't it? So why not just go ahead and create the HTML too.

    --ed

  16. #16
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There would be no noticable speed loss from the class. It's lean and small - a piece of plankton in the whale-like maw of a modern computer. We're not coding on Sinclair Spectrums.

    The point of "keeping html out of php" is to separate presentation and biz logic. If it's a class for presenting html, html inside is just fine (xhtml might be better still ie <br /> not <br>).

    OOP isn't primarily about code re-use. It's about creating a good, modular design which is easy to understand and work with. With the implementation details hidden behind an interface, you can forget all about it and concentrate better on the task at hand. It's the same sort of principle as organising a book into chapters and sections. Much harder to read without it - and harder to swap bits in and out.

  17. #17
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff
    There would be no noticable speed loss from the class. It's lean and small - a piece of plankton in the whale-like maw of a modern computer. We're not coding on Sinclair Spectrums.
    Recent threads tend to contradict this:

    Quote Originally Posted by bmatheny
    Update. We have released the new site, after using many of your helpful replies. Interestingly, the bottleneck has not been NFS and the bottleneck has not been sessions (400,000 in a 24 hour period). The bottleneck actually seems to be the code. We wrote this site in 100% OO PHP4. We now have 5 webservers, 1 sql server, and 1 nfs/web server. Our old setup (using php nuke!) required 2 web servers, 2 sql servers, and 1 nfs/web servers. Our code is absolutely more efficient, easier to modify/read, and simply better. However the overhead in using a pure OO php setup is more than I thought it would be.
    All claims about high-performance machines aside, it's still possible to write code that performs poorly.

    =Austin

  18. #18
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Austin_Hastings
    Recent threads tend to contradict this
    =Austin
    (link broken)

    I sometimes make remarks about Hubble Space Telescope territory - but you wouldn't notice any difference using the above class even WITH a Hubble Space Telescope... It's just a fn and a few vars.

    This sort of question crops up regularly and it's important to understand that alleged performance penalties with OOP usually exist more in the imagination than in reality. The benefits of well-designed code vastly outweigh a micro-second or two here and there.

    Amongst other things, it's much easier to find and fix bottlenecks with a good, modular design.

  19. #19
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff
    (link broken)
    Sorry about that. It's the Scalable Sessions thread, also in Advanced PHP. Let me try again.

    http://www.sitepoint.com/forums/showthread.php?t=181425

    This sort of question crops up regularly and it's important to understand that alleged performance penalties with OOP usually exist more in the imagination than in reality. The benefits of well-designed code vastly outweigh a micro-second or two here and there.
    Take another look at this part of the original quote:

    Quote Originally Posted by bmatheny
    We wrote this site in 100% OO PHP4. We now have 5 webservers, 1 sql server, and 1 nfs/web server. Our old setup (using php nuke!) required 2 web servers, 2 sql servers, and 1 nfs/web servers.
    It's not a second or two here or there, and it's not some 'noob' that heard a net.rumor and is wasting the time of everyone here. It's a real user, that has a real application, that is serving real traffic. And his(*) concrete observation is that PHP4+OO has a real negative impact.

    In the U.S. government, there's a saying: A billion dollars here, a billion dollars there, and pretty soon you're talking about real money.

    If you want to claim that the OO penalty is "a microsecond or two here and there", well, I think they've begun to add up.

    Quote Originally Posted by McGruff
    Amongst other things, it's much easier to find and fix bottlenecks with a good, modular design.
    I absolutely agree with you on this.

    =Austin

    (*) I am assuming bmatheny is a 'he'.

  20. #20
    SitePoint Zealot Selatos's Avatar
    Join Date
    Aug 2002
    Location
    USA
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well personally I wouldn't use a class there, because I'm not of the belief that everything that is on the page should be an object, I'm a fan of mixing procedural and OO programming to get the advantages that OO programming has to offer and the specificness of procedural code. I just feel that a Link class isn't important enough to design an object around it, despite any accessor methods. I mean really, am I going to want to check where a link pointed to halfway up the page? I can't come up with a situation where it would be important to have all of the links that I create saved as objects that I can access anywhere on the page.

    All claims about high-performance machines aside, it's still possible to write code that performs poorly.
    I agree totally, because if you feel the need to make everything an object, you will go out of your way to code a class for it and eventually incur several overheads that could have been avoided had you taken a simpler, more direct route. I believe OO has power and its place in the programming world, but not everything should be restricted to OO. Procedural code can be very effective and efficient for smaller tasks.

  21. #21
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just feel that a Link class isn't important enough to design an object around it,
    Well, yes, though you shouldn't think of an object as you've described (quoted) as such ?

    For example, you may have a bunch of external links, stored in the database. But then again, if you apply OO Programming to the equation, it'd be easier and simpler to give those external links a category for example

    You could do just about anything with an object (links) a lot easier with OO than procedural, like OO just makes life a lot easier in general in fact

  22. #22
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    NativeMind - Thanks a lot for explaining more on these patterns

    Looked at Harry's example (latest) though still find it kind of difficult to work it ? But I'll be paying more attention to this pattern in the future, rather than just EXTENDing child classes

    On the Visitor pattern, I read a PDF a while back on it (Java) though, obviously I've still a lot to learn on this pattern as well

    The two seamed very much alike.

  23. #23
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by Selatos
    A) Will i use this code over and over again?
    B) Is this class something that I can easily modify to make it applicable in similar situations?
    C) Is what it accomplishes important enough for me to make a class for it, instead of just coding it every time?
    I always thought that these would be the issues I would face, but it never came to pass. Or rather when I did try to resolve these issues ahead of time the result was a bit of a mess. Nowadays classes for me are a means of defining and expressing the underlying design. That design can be evolved, or drawn from experience or be taken from the business model/metaphor. The designs get reused, but I hardly ever reuse code across projects.

    Within the projects the classes are allocated responsibilities so as to reduce repetition, to make the system flexible by having a decision in only one place and to expose enough architecture for testing. For myself there is no thought of the future when I do this. I am just using them as way of organising the code.

    What makes a good class? A strong job or role. Programming is about automation of work. You cannot do work without the "do" part. What does the class do? If I cannot come up with an answer for this, then I probably won't code it very well. It's very important to get the name right on a class for the similar reasons. A class that is too passive won't pull it's weight anyway, but mainly it falls into this lack of having a role.

    Does the potential link class have a job to do in his system or is it just data?

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  24. #24
    SitePoint Zealot Selatos's Avatar
    Join Date
    Aug 2002
    Location
    USA
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well your idea is similar to "C", just worded differently.
    What makes a good class? A strong job or role.
    I think that is an excellent way to describe the purpose of a class, and in this case I feel that the link class would just be data storage, and not worth a class.

    Thanks to everyone, this has helped me understand when to apply OO Programming to a situation and helped me define what makes a class worth creating.

    Since we are on the topic of classes, I figured I'd ask for some feedback on my database class, which I wrote yesterday(does it meet the suggested requirements for a class, is it well written?):
    PHP Code:
    <?php
    //database.class.php
    //Property of Greg (Selatos)
    //Unauthorized use, distribution, and copying is prohibited
    //make sure the template is included or the die() wrapper statements will cause an error
    require_once("template.class.php");
    //the database class, one instance is used for all the script and is used as a
    //wrapper for all the query classes etc. Handles multiple queries via queryIDs
    //some definitions to save space
    define("DB_CONNECT_FAILURE""A connection to the database was unable to be established.
    Please refresh this page to try again."
    );
    define ("DB_GONE_MISSING""The Database that was selected could not be found. Please refresh to try again.");
    class 
    Database {
        var 
    $query//array of Query objects
        
    function Database($user$pass$database$host "localhost") {
            
    $dbcnct = @mysql_connect($host$user$pass) or $template->panic(DB_CONNECT_FAILURE);
            
    $db = @mysql_select_db($database$dbcnct) or $template->panic(DB_GONE_MISSING);
        }
        
    //create a query
        //the $sql statement should have %p in all of the positions where a parameter
        //will be placed. add the parameters to the array in the order they will be
        //placed in the query
        
    function createQuery($sql$parameters) { //$parameters is an array
            
    $queryID count($this->query);
            
    $this->query[$queryID] = array("query" => new Query($sql$parameters), "position" => 0);
            return 
    $queryID;
        }
        
    //iterator for queries
        
    function iterate($queryID = -1) { //default value is the ID of the last created query
            
    if ($queryID == -1) {
                
    $queryID count($this->query)-1;
            }
            return 
    $this->query[$queryID]["query"]->getResult($this->query[$queryID]["position"]++);
        }
        
    //change database
        
    function changeDatabase($database) {
            @
    mysql_select_db($database) or $template->panic(DB_GONE_MISSING);
        }
        
    //function to free memory and release a query
        
    function releaseQuery($queryID = -1) { //default value is the ID of the last created query
            
    if ($queryID == -1) {
                
    $queryID count($this->query)-1;
            }
            unset(
    $this->query[$queryID]);
        }
    }
    //create the query class
    class Query {
        var 
    $sql;
        var 
    $resultset;
        function 
    Query($sql$parameters) {
            
    $count 0;
            
    //cut up the string so that ereg_replace doesnt replace all the symbols
            
    $sqlA explode("%p"$sql);
            
    $sql $sqlA[0];
            while (
    $count count($parameters)) {
                
    //make the parameter safe
                
    $parameters[$count] = mysql_escape_string($parameters[$count]);
                
    //add to query
                
    $sql .= '"'.$parameters[$count].'"'.$sqlA[$count+1];
                
    $count++;
            }
            
    //execute query
            
    $this->sql $sql;
            
    $result mysql_query($sql);
            
    $this->resultset = array();
            
    $count 0;
            while (
    $temp mysql_fetch_array($result)) {
                
    $this->resultset[$count] = $temp;
                
    $count++;
            }
        }
        
    //function to show the query(debugging purposes)
        
    function getQuery() {
            return 
    $this->sql;
        }
        
    //function to return the next array in the resultset
        
    function getResult($pos) {
            return 
    $this->resultset[$pos];
        }
    }
    ?>

  25. #25
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The benefits of well-designed code vastly outweigh a micro-second or two here and there.
    its not a microsecond or 2 , a full class implementation (e.g done `properly`with said link classes implementing HTML generation classes implementing a base uber_object etc) is far more than a microsecond or 2 and if your site (we are talking websites here BTW) has any sort of traffic at all those microseconds are concurrent.

    If you are using a byte-cache , or a compiled language like JAVA (which PHP is not for those who had'nt noticed) then fair enough ... for the vast majority of PHP websites today it is another matter.

    Perhaps one can say 'I have $var cache' so it does not matter ..? perhaps?.

    looking into my crystal ball, I forsee a post in the near future telling us its OK and to just throw more hardware at it .... you can't argue with that since it totally ignores the issue.

    I use OO more and more each day & love some of the more basic improvements that ship with PHP5, but very rarely at the sharp end , the end that hopefully gets called $n times per second.


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
  •