1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP classes

Discussion in 'PHP' started by stephan2307, Oct 19, 2019.

  1. #1
    I am trying to improve the way I code and I came up with the following question but couldn't really find an answer.

    Lets assume you have an online shop would it be a good idea to have 2 classes that relate to products.

    1. product: this will be the actual product and will have properties like sku, price, name, description, etc and methods like save or delete ( I guess these would be for admins only )

    2.products: this will be a class that deals with returning a number or product objects when for example you search or browse through the online shop. I would return objects of products rather than just an array.

    Is this a good way to do this?

    Thanks for any feedback
     
    stephan2307, Oct 19, 2019 IP
  2. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #2
    The question can't be answered as no one knows how your application is designed. You generally will need separate code to use a class if you are creating a class. If you are not using a single entry application then you could have a products.php page that shows a list of products and a product.php page that shows the details of an individual product. You probably could use 1 separate class for those scripts to pull. Most applications use a single entry point. For instance index.php then that file uses classes to display pages, fetch database entries etc. Your question is hard to answer without knowing how your application is designed.
     
    NetStar, Nov 1, 2019 IP
  3. dzikipaw

    dzikipaw Well-Known Member

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    106
    #3
    Post the code otherwise it's all academic
    Stackoverflow.com is your friend
     
    dzikipaw, Nov 2, 2019 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    In PHP, there is rarely any benefit of using class over functions.
    Purpose of using class is that it makes it easy to maintain and classify multiple functions at one place. May be use it later in some other script.

    Like database connection methods/functions for example.
    Put all your database methods in one class, and use the class to fetch and insert data into database.

    You can later use this same class with some other script of yours.
    These will be easier to read and manage.

    Generally, when we use functions, we tend to put them all in one big file, which makes it difficult to export those functions to some other project.
    However, if you have separate files for database connection functions, and a separate file for form layout functions, then its almost the same thing.

    Another advantage of using class is that you have less risk of overlapping functions.

    Suppose you have a class called "MyDatabase", which has a method called "output"
    You also have another class called "HTML_Layout", which also has a method called "output"
    These 2 methods with same name will not clash, will not break your code, if used in the same project.

    But if you had 2 function files, "MyDatabase.php" and "HTML_Layout.php"
    both having a function called "output"
    and if you plugin both these files in the same project, php will throw an error.
    Or worse, only one function will get defined, whichever comes first, this is if you used function_exists check for all functions.
    The script will start behaving weird...

    Classes are mostly useful in coding languages which compile code.
    Developer can make a class, compile it, and then distribute it, with a manual on how to use the methods available.
    No need to show the code to the coders who are using that class.
    Its all compiled in machine language. Developers simply use the available methods using the manual provided.

    But this is not the case with PHP. PHP is open source, compiled at run time.
    Anyone who has the class file, can see the code, modify it, use it, do just about anything.


    Your approach in your project is good.
    2 separate files, one for product details, another for admins.
    However, not a necessity in this project.
    You can as well do with 2 function files...
     
    JEET, Nov 2, 2019 IP
  5. Harry H

    Harry H Greenhorn

    Messages:
    27
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    23
    #5
    False. Classes are used because they have inheritance and hide implementation logic with private members/functions. Big chunks of linux libraries are written in C code without classes which is then compiled. I can access those functions by their function name as easily as a class. For instance, sqlite API doesn't have classes. Inheritance is a benefit because it means you can make a generic object and then later add some extra features relevant to another object without having to write that class from scratch. That's just as relevant in PHP as it is with a compiled language. Haskell is also a compiled language and purposely avoids the class paradigm.
     
    Harry H, Nov 2, 2019 IP
    JEET and sarahk like this.
  6. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #6
    ...I stopped reading after this.

    People if you are going to comment make sure you know what you are talking about. Newbies always make this mistake which brings down the quality of advice here...
     
    NetStar, Nov 2, 2019 IP
    sarahk likes this.
  7. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #7
    As an old school coder classes were a very useful way of protecting your code in the days before namespaces.

    Any website that grows bigger than a handful of pages will probably benefit from the use of classes.

    Dig under the covers of WordPress, Joomla, any of the frameworks and you'll find classes are an integral part of the code organisation.

    As for the OP's project I'd need to know more but chances are that they're on the right track.
     
    sarahk, Nov 2, 2019 IP
  8. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #8
    NetStar
    You can stop reading wherever you want, but do read the question also first.
    He simply wants 10-12 functions in his basic ecommerce website.

    Also read my last lines in that same comment.

    Harry H
    I agree classes can be extended to add more functionality to existing code.
    I code in JAVA too, I know classes.
    But this is not what he wants in his 2 page ecommerce website...
    Neither is he redistributing his classes to other coders "to extend" and use in their projects...
     
    Last edited by a moderator: Nov 3, 2019
    JEET, Nov 2, 2019 IP
  9. Harry H

    Harry H Greenhorn

    Messages:
    27
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    23
    #9
    I'd just like to add to this that choosing functions over classes isn't a simple, large projects benefit from classes and small ones are fine with functions. This is an open question in the programming community to this day. Nobody knows for sure which is more productive. Some languages like Java & C# are opinionated that classes are more productive. Other languages most notably Haskell are opinionated in the other direction completely. Then there is a lot that let you choose for yourself like PHP, javascript (js has a strange history here, and it's entire OOP structure is completely different from almost anything else except some legacy languages).

    The extending classes part is more useful from a design perspective, it's not really about distributing classes for others to extend. In fact when distributing things to extend, I'd say it's just as common to supply an interface (In functional programming like haskell that interface would often become a lambda closure function, possibly enclosed in a larger data type... lambda functions are quicker to write, but interfaces generally have a stricter structure and cannot be curried. Hence my point about it being an open ended question as to which is more productive long term)

    Let me give an example of why OOP is beneficial in an ecommerce store. I can make an object BaseProduct. That will handle 90% of the things I want to do with my products, but then I decide MultiColorProduct is a new object that extends it and will handle drawing controls for color selection. It's a contrived example but the benefit is not just when I go to distribute the class for other people to extend their own products. It enforces a good separation of code into two clearly defined files, with exactly what they do being quite obvious. Again not disagreeing with you here on functions potentially being a good fit for a small project like his, I could write the exact same code with functions and closures and make it just as readable. However there are a lot of people who benefit from thinking about their project in an object style.

    The part I mainly disagreed with though, was the compilation comment. Internally when you compile a c++ class it mangles the class member name and then translates it into a raw function that accepts the object pointer as the first argument. Object inheritance is then handled with a lookup table attached to the object in memory. There is very little difference in how I would link to a library written with only functions vs one written with classes (except you need a c++ compiler/linker to to use the classes because the mangling and such is compiler specific and complex which is why rust can link to C but not C++). Rust functions can be exported to C applications with no real problems. I can also extend somebody else's class without requiring the source code as long as I have the header files in C++.

    Java isn't fully compiled for a x86-64 CPU until it is run either. There is a ton of meta data supplied with a java program from which you can rebuild the source quite closely to what it started as. It is compiled to run on a virtual CPU that doesn't exist, and then compiled again when run onto the real CPU. So that gap between interpreted & compiled is constantly moving. To highlight my point, historically javascript is an interpreted language and java is accepted to be a compiled language. However today both use a JIT compiler by the time they are actually run on the end user's PC. So are there still big differences between java code and javascript code?

    Not a rhetorical question either, if your code runs on a JIT is it a compiled language? PHP has a JIT now too. Just because libraries are supplied with source in js & php doesn't mean many people are actually changing them, just as they wouldn't/couldn't with a compiled language. It's an update/debugging nightmare to change somebody else's library.
     
    Last edited: Nov 3, 2019
    Harry H, Nov 3, 2019 IP
    JEET and sarahk like this.
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #10
    @NetStar is right in that we don't have enough information to give a rational answer.

    @JEET is right in that we're not hearing anything that warrants the use of Objects at all ... or even functions for that matter beyond what you would/should have either way. They could in fact cause more harm than good by taking something simple (database data) and make it pointlessly bloated, convoluted, and harder to manage.

    It could also be your data needs something like getter/setter protections or optimization so you're not wasting time gathering excess database info or performing recursive or other wasteful operations, where objects could reduce the complexity and memory footrpint.

    I'm not seeing how/why the function vs. object debate even plays into this, because zero information on how the data is stored, what operations are performed, how the user enters/exits the PHP via URI, or dozens of other determining factors.
     
    deathshadow, Nov 4, 2019 IP
    JEET likes this.