SitePoint Sponsor

User Tag List

Page 1 of 4 1234 LastLast
Results 1 to 25 of 82
  1. #1
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)

    Class design like I'm using Java

    I am in the process of planning out a custom shopping cart for a client and I think that OO is the best way to go. I am in the mindset that I can design this like I would design a Java application. Is that the right way to go about it? I am going to be using PHP 4.3.9, so I know that there are some limitations as to what I can do.

    My thinking is that I can have a class like:

    PHP Code:
    class customer {
          
          function 
    customer()
          {
          }
          
          function 
    getCustFName()
          { 
          }

          function 
    getCustLName()
          {
          }

          function 
    setCustFName($cFirstName)
          {
              
    $this->custFName $cFirstName;
          }

          function 
    setCustLName($cLastName)
          {
              
    $this->custLName $cLastName;
          }
    //etc.

    With PHP should I be that narrow with my methods or should I broaden out the functionality to maybe something like getCustName()?

  2. #2
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not a fan of OO for OOs sake. You can code like you have above if if fits within your application. However, PHP has things like associative arrays that can do the above more efficiently and with a lot of built-in support.

    If you want to use Java, use Java. You can build web apps fine in either Java or PHP.

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2003
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am in the mindset that I can design this like I would design a Java application.
    I guess that depends on how you would design your Java applications.
    PHP is dynamically typed which gives you greater flexibility than Java. But you are of course not forced to take advantage of that, you are free to design how it would be done in Java. Though you have to do a lot of things "by convention" because the language (or the compiler) doesn't enforce it.

    should I be that narrow with my methods or should I broaden out the functionality to maybe something like getCustName()?
    What do you mean by narrow? While sometimes it is unavoidable to have getters and setters for most member-variables, the important methods are the ones that actually do stuff. With many getters and setters, there's always the risk, that you might put the code that works with the data some other place, which will lead to high coupling and low coherence, which is not desirable. This goes for any OO-Language.

    Your above example-code is too small to actually comment on it. It looks more like an Abstract Data-Type (or struct [C++] or record [Pascal]).

    Or were you merely asking about syntax? http://www.php.net/manual/en/language.oop.php
    Or asking for an introduction to OO in PHP? http://www.sitepoint.com/article/object-oriented-php

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    norway
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Everything doesn't need to be a class.
    Everything doesn't have to use accessors.
    I use classes and functions interchangeably, and it works fine.

    But, if you come from a java-background you don't really have to change the way you develop.. If you have lots of classes you might notice a hit if you got the visitors, but it's not really much to concider because then you'd use caching and possibly php-compilers anyway.

    But beware, php4's oo-implementation (and general design) isn't exacly up there with java..

    With PHP should I be that narrow with my methods or should I broaden out the functionality to maybe something like getCustName()?
    If that makes sence, then yes. But that really isn't a php (or any language)-dependant question. Alternatively you could keep those and add getCustName which returns "$this->getCustFName() . ' ' . $this->getCustLName()" - but this is pushing it in php imo..

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    norway
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by arborint
    f you want to use Java, use Java. You can build web apps fine in either Java or PHP.
    *nod*
    It seems to me a lot of people are trying to apply patterns and framework to every little variable in php nowadays, which can end up with something that is a lot bigger and harder to maintain.
    Just because everything is an object doesn't make it easier to maintain - in fact, it is much easier to end up with something nonorthonogal, with all the inheiritance, parent-method overloading etc.. (But yes, if you know what you are doing, you can end up with something very good).

    Seems to me this is the next "big" thing after template-"engines" - just look at the amount of mvc-related posts. "The devil is in the details" someone said there, and boy was he right

  6. #6
    SitePoint Guru
    Join Date
    Dec 2003
    Location
    oz
    Posts
    819
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Every OOP language is pretty much the same. There are some minor differeneces, but the same prinicples apply. The patterns are the same. N-tier design is the same. Templating is based on the same concepts. Anyone who tells you that you should change your mindset completely from using Java is full of s***

    The only main difference is that php is dynamically typed (actually i think its all stored as strings pre-php5 - but not sure on that). PHP programmers are too lazy to use getters and setters usually. Just becasue alot of people in this forum will agree on that doesnt make it right.

    It really yanks my chain when people here say it's so different to Java. All OO languages are derived from the same one - they are based on the same syntax, and hence the same design would be used independant of the particular language.

  7. #7
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lazy_yogi
    It really yanks my chain when people here say it's so different to Java. All OO languages are derived from the same one - they are based on the same syntax, and hence the same design would be used independant of the particular language.
    I mostly agree with you, but the comment about associative arrays in PHP is a good point, though. The equivalent in Java (maps) is a lot more cumbersome. If all you want is a dumb data holder, an array is as good as an object in PHP. If you set error handling to E_ALL, you can even get a message if you try to access a key that hasn't been defined.

    The problem may be if you start off with an array and then decide you really need to change it to an object later. If you've used the array extensively in a production application, that's relatively hard to refactor.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  8. #8
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have to say that I disagree with lazy_yogi on this. The actual language constructs are similiar in most languages. But PHP and Java have such totally different libraries and environments that how you program is very different. Dynamic vs strongly typed is really only a small part of it.

    Getters and setters are a prime example of OO brainwashing. Give me a reason to use getters and setters in the example above other than a love of function call overhead. Would you write this code:
    PHP Code:
    $myvar 0;

    function 
    setMyVar ($var)
    {
        global 
    $myvar;
        
    $myvar $var;
    }

    setMyVar(1); 

  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    norway
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In java you could need rtti, you could implement interfaces, use abstract classes, exceptions, polymorphism, private/protected members..

    PHP4 lacks a lot of this, and although the general design could well be identical, you should still know the differences and how to exploit them (like the obvious fact that php is dynamically typed). Some languages have features others don't - take advantage of it.

    Though these things are mostly implementation details, when they add up, it could affect some decisions.

    Quote Originally Posted by lazy_yogi
    Every OOP language is pretty much the same.
    To me, smalltalk, obj-c, c++, php4 and perl aren't "pretty much the same"; just because a language supports the "class" keyword doesn't automagically make it equal to the rest.

    Quote Originally Posted by Aphenitry
    If that makes sense, then yes. But that really isn't a php (or any language)-dependant question.
    Anyway, I'm not even sure if you were replying to my posts lazy_yogi, and I think we agree.

  10. #10
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    norway
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aborint
    ...Give me a reason to use getters and setters in the example above other than a love of function call overhead....
    Well the whole point is that to program to an interface, not an implementation - wether or not it just returns a variable or does some fancy calculation is irrelevant. Perhaps it even calculates it only on the first "get", then caches the result for lates use.

    Atleast, that's what they'd tell you (and I'm sure you already know this). In small things such as basic webpages, I think this is getting a little fanatic

    I hate going through a bad implementation of "get/set"-hell instead of a quick and to-the-point implementation - getting oo right is hard.

  11. #11
    SitePoint Zealot
    Join Date
    Feb 2003
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lazy_yogi
    Every OOP language is pretty much the same. There are some minor differeneces, but the same prinicples apply.
    I think what you meant to say is that you either don't know about the differences or that you don't care, both of which is fine, but it doesn't mean the differences aren't there. Depending on what you are comparing the differences can be pretty big.

    If you take a look across the OO-Literature, you'll see that there isn't even a clear definition on what OO is. Are classes a necessity? Then what about prototype-based OO languages? Is Inheritance a necessity for OO or is it merely a way of achieving polymorphism? In the recent years a lot of researchers and practitioners frown upon implementation inheritance and put subtyping up there as the "one rightway", which doesn't really jive with a the experience of people from dynamically typed languages (see e.g.the Smalltalk Class hierarchies w.r.t. Dictionary inheriting from Set).
    I've seen books where garbage collection was declared as one of the key elements of an OO-Langauge.
    It took ~8 years for Java to finally get genericity, which is necessry for certain types of flexible OO designs (e.g. with Collections). And the Problem that genericity solves don't even exist in dynamically typed languages like Smalltalk, Python, PHP.
    And I could go on and on...

    The patterns are the same.
    What do you mean by "The patterns"? There are quiet a few patterns around that are needed to get around the deficiencies of certain languages (not OO in general); if you happen to be programming in a language that doesn't have that deficiency, you won't need the pattern. That's why it's important to really understand the patterns, instead of applying voodoo-programming where you're just throwing patterns at your code because you saw some other person do it.

    Anyone who tells you that you should change your mindset completely from using Java is full of s***
    I don't think anybody suggested that, so please don't try to put up strawmen. What people were saying is, that you'll get the most elegant solution if you try to take advantage of the features the language can offer you.

    It really yanks my chain when people here say it's so different to Java. All OO languages are derived from the same one - they are based on the same syntax, and hence the same design would be used independant of the particular language.
    What has syntax to do with Design? If languages were as irrelevant as you describe them to be, there certainly wouldn't have been any point in inventing them in the first place. I am sure you've heard about http://en.wikipedia.org/wiki/Sapir-W...ming_languages

    A well-known epigram by Alan Perlis states that "a language that doesn't affect the way you think about programming, is not worth knowing". Think about it...

    Wether all of this is any relevant to any particular person certainly depends on the POV. If one just want to get stuff done, and doesn't care about exploring, then it is possible to apply one thing that has worked in certain circumstances to any other situation and get away with it just fine. Just as there are a lot of people programming procedureal in Java and C++.

  12. #12
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    The off topic (somewhat) conversation is actually quite interesting.

    I have to use PHP for this project (it's what the clients server has), so Java is not an option.

    His volume of purchases is not going to be super high, but my goal with programming the cart with OO was to make it as 'packagable' and manageable as possible. I want to be able to finish writing the product and say "We can use this for other clients because it is simple to port".

  13. #13
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I started out with PHP, then learned OOP and wrote functions and classes interchangeably. All worked fine, but there were GOD classes and it really was hard to figure out what does what in the zoo.

    Now I learned Java and therefore I can say: Do as much as possible like you'd do it in Java! It really is the most elegant way to do it IMO.

    Here I'd change your code a bit:

    PHP Code:
    class customer {
          
          function 
    customer()
          {
          }
          
          function 
    getCustFName()
          {
          }

          function 
    getCustLName()
          {
          }

          function 
    setCustFName($cFirstName)
          {
              
    $this->custFName $cFirstName;
          }

          function 
    setCustLName($cLastName)
          {
              
    $this->custLName $cLastName;
          }
    //etc.

    To:

    PHP Code:
    class Customer // <-- start with a capital letter
          
    var $firstName '';
          var 
    $lastName '';

          function 
    Customer() {
          }
          
          function 
    getFirstName() { // <-- no need to have the word 'Cust' here..we are already in the Customer class
          
    }

          function 
    getLastName() {
          }

          function 
    setFirstName($string) {
              
    $this->firstName $string;
          }

          function 
    setLastName($string) {
              
    $this->lastName $string;
          }
    //etc.


  14. #14
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tips DarkAngel!

    I am not going to actually be using Java, my question was more of should I use the Java mindset going into this by abstracting as much possible and coding in heavy OO.

  15. #15
    SitePoint Addict
    Join Date
    May 2003
    Location
    Calgary, Alberta, Canada
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arborint
    Give me a reason to use getters and setters in the example above other than a love of function call overhead.
    In a presumably large application like a custom shopping cart there could be many reasons to use an object like above.

    One may be to decouple the database from the application. If we use an array returned by mysql_fetch_array throughout the entire application, everything becomes tightly coupled to the database column names.

    Quote Originally Posted by R. U. Serious
    What has syntax to do with Design? If languages were as irrelevant as you describe them to be, there certainly wouldn't have been any point in inventing them in the first place. I am sure you've heard about http://en.wikipedia.org/wiki/Sapir-W...ming_languages
    I just want to point out this paragraph from the link:
    Quote Originally Posted by http://en.wikipedia.org/wiki/Sapir-Whorf_and_programming_languages
    This is not universal, however. Some programmers find the task of algorithm design largely the same, regardless of the programming language used - and, with the overwhelming dominance of imperative languages in commercial applications, the supposed diversity of programming languages is often a case of slightly different syntax for essentially the same set of programming language constructs.

  16. #16
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The question is, why would you not want to use OOP? I'd disagree with a comment above that OOP will make the app harder to maintain. In fact, the opposite is the case.

    Suggested menu: a double helping of OOP served with design patterns and a side order of useful libraries like AdOdb, Eclipse, WACT etc, and a waiter on hand to keep your glass constantly topped up with unit tests.

    Objects don't persist in php but I don't know enough about java to say how that might impact on the patterns you're used to.

  17. #17
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    norway
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Brenden Vickery
    One may be to decouple the database from the application. If we use an array returned by mysql_fetch_array throughout the entire application, everything becomes tightly coupled to the database column names.
    Things like these are okay to think about in bigger projects, but in the average website I feel these abstractions are overkill. I've made "tightly coupled" websites like this for years, and I have never had any problems with it. Ofcourse, I do the technical part alone, but regular websites doesn't require much more imo.
    Quote Originally Posted by Hartman
    I am not going to actually be using Java, my question was more of should I use the Java mindset going into this by abstracting as much possible and coding in heavy OO.
    Sure you can, as previously commented, just learn what php4 supports and not first.

    And McGruff, I didn't say it automatically makes it harder to maintain, but it doesn't automatically make it easier to maintain either. Doing real object-oriented design is hard. By the way - you can use all the things you mentioned without knowing what patterns are or using oop at all other than to instantiate the objects and invoke their methods. It doesn't have to be objects to run automated tests, either.

    Doing a mvc model2 implementation in php of a single-developer webpage with a news-section and a few info-pages will make it harder to grasp and maintain than a simpler (i.e more suited to the problem) approach - oop or not.

    Anyway, I use oop for my php5 projects, unit-tests and everything, I'm simply saying know when to stop.

  18. #18
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The question is not whether you should use OOP in PHP. Most everyone agrees you should use OOP for all the good reasons to use it. The question is whether to mimic Java implementation for PHP implementation. I think no. Give me a single helping of OOP, a helping of Structured Programming, and unit tests as appropriate.

    Let's face it, the people writing about sophisticated software ideas are not thinking about the PHP Way. They are for the most part involved in J2EE or .NET (along with a smattering of OO experimentation languages). These are not web centric languages. Both Sun and Microsoft have twisted the web centric idea to push people to their own stack. Neither is a friend of the PHP Way.

    We have glimmers when people like Rasmus and the Zend guys talk about PHP, but they are drowned out by the GoF worshipers. I have said that GoF are the gurus of where we have been, not where we are going -- and I believe that. The reason you get such hostile comments from Java programmers about PHP is that PHP's incredible success proves that the GoF's religion is not the only truth.

  19. #19
    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 arborint
    Let's face it, the people writing about sophisticated software ideas are not thinking about the PHP Way.
    But at least they are thinking.

    The "php way" reflects the ease with which you can pick up the language. Go to any script repository and randomly pick a php program: I can almost guarantee it will be a dog's breakfast of bad design - if indeed there was any design at all. Even PEAR is a major offender and that's supposed to be a core php library.

    OOP design can be subtle and profound. It's a difficult thing to learn. The pay off is in easier maintenance, more re-use, and generally just better and more reliable software.

    I help to maintain a forum which uses phpBB. I wouldn't wish that on anyone.

  20. #20
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you miss the larger web centric idea that PHP is a publishing meduim not a subtle and profound religion. Go to any script repository or look at phpBB and you will notice the powerful fact that there are many useful scripts that are DONE. They work; the do something useful.

    Code beauty and intellectual satisfaction are irrelevant to all but a small group of programmers. Maintainablity is important, but less important for most web applications than other types of applications.

    Software that is used is the most beautiful thing. And the fact that most of the useful PHP applications (phpBB, phpMyAdmin, WordPress, etc.) are coded in a way that does not make you happy may be telling you something.

  21. #21
    SitePoint Addict
    Join Date
    May 2003
    Location
    Calgary, Alberta, Canada
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Aphenitry
    Things like these are okay to think about in bigger projects, but in the average website I feel these abstractions are overkill. I've made "tightly coupled" websites like this for years, and I have never had any problems with it. Ofcourse, I do the technical part alone, but regular websites doesn't require much more imo.
    Then what youre saying applies to how you would develop with Java. Im not saying youre wrong, Im just saying that its really not that different:
    PHP Code:
    ResultSet rs stmt.executeQuery("SELECT a, b, c FROM Table1");
    while (
    rs.next()) {
        
    System.out.println("ROW = " rs.getString("a") + 
                                
    " " rs.getString("b") + 
                                
    " " rs.getString("c"));
    }

    $rs mysql_query("SELECT a, b, c FROM Table1");
    while (
    $row mysql_fetch_array($rsMYSQL_ASSOC)) {
       print(
    "ROW = " $row["a"] . 
                  
    " " $row["b"] . 
                  
    " " $row["c"]);

    The decision of when to use a Customer object or not isnt a question answered by the language youre using but a question answered by the application youre building.

  22. #22
    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 arborint
    Code beauty and intellectual satisfaction are irrelevant to all but a small group of programmers. Maintainablity is important, but less important for most web applications than other types of applications.

    Software that is used is the most beautiful thing. And the fact that most of the useful PHP applications (phpBB, phpMyAdmin, WordPress, etc.) are coded in a way that does not make you happy may be telling you something.
    I'm not talking about some kind of aesthetic deal - those are your words. Design exists for very practical reasons and produces very real benefits.

    phpBB certainly tells me something: it speaks volumes about the maintenance difficulties which are created by bad design. Quite frankly, I'd rather rewrite the whole damn app than attempt to maintain it as it is.

    Maintenance is a huge issue. I'm constantly thinking about this when I'm coding.

  23. #23
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then what youre saying applies to how you would develop with Java.
    Sort of true, but you left out the 20 lines above you Java example and the 2 lines above your PHP example. The libraries and environment are different.

  24. #24
    SitePoint Addict
    Join Date
    May 2003
    Location
    Calgary, Alberta, Canada
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didnt think those 20 lines were relevant to the topic. Why are the libraries and environment a factor in deciding whether or not to create a Customer object in Java?

  25. #25
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It was the 20:2 ratio of lines I was pointing out.

    Why are the libraries and environment a factor in deciding whether or not to create a Customer object in Java?
    Your example is brilliant. It clearly shows that you CAN write the Java example in PHP, but you CANNOT write the PHP example in Java.

    And writing the Java example in PHP would only achieve slower code. In a more complex example there could be a reason, but not for the simple stuff that most PHP scripts need to do. When it comes to clarity or maintainablity are the 50 lines better than the 5 lines?


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
  •