Web Analytics Made Easy -
StatCounter Even though PHP is obect oriented - CodingForum

Announcement

Collapse
No announcement yet.

Even though PHP is obect oriented

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Even though PHP is obect oriented

    I have heared that even though PHP is obect oriented,
    I do this in a diffrent way that c++ do.
    (i have heared as all classes are added as inline codes in every page that use it)
    Is it true?
    Do it have any dissadvatage?

    Sujith

  • #2
    My C++ is a little rusty but I'm pretty sure even in C++ you have to include the relevant code files.
    You do the same in PHP.
    IE You might have a cat class and the code might be in cat.class.php
    On any page you needed cats you would
    PHP Code:
    include('cat.class.php'); 
    Then that script would have cats available to it.

    The disadvantage of PHP OOP is speed objects are slower to use than functions because of the overhead in creating the object.
    Last edited by NancyJ; Oct 5, 2006, 03:25 AM.
    http://www.hazelryan.co.uk

    Comment


    • #3
      In PHP, classes don't need to be in a file all on their own, they can be mixed with procedural code.
      For anything but the smallest application though, it makes things much easier to do so
      My thoughts on some things: http://codemeetsmusic.com
      And my scrapbook of cool things: http://gjones.tumblr.com

      Comment


      • #4
        The downside to include(class.file.php), include_once(class.file.php) is that - what if you have 4 or 5 classes to include? what about a file full of functions?

        I'd suggest a global.config.php file which contains all of your commonly used class files as well as all of your global defines (db information, etc).

        This way, even if you haven't put together a decent template system for your site, you still only have to do a single include to pull in all the functionality you desire.

        PHP Code:
        <?php require_once('framework/global.config.php'); ?>
        Active PHP/MySQL application developer available for immediate work.
        syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready

        Comment


        • #5
          Oh and a comment about "the speed of object creation" - he's talking milliseconds. Not something that will be noticable by users of your site unless you happen to be creating a million objects, then they might have to wait a second or two.

          It's hardly a reason to even consider not using classes - shouldn't even be mentioned - and if that is the only disadvantage, full speed ahead with object creation. Everything shouldnt be about speed, code flow/structure and usability/maintainability far out weigh any implied speed loss.
          Active PHP/MySQL application developer available for immediate work.
          syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready

          Comment


          • #6
            Yes, the difference is miliseconds - but it all adds up.
            see http://www.webmasterstop.com/56.html
            Now I'm not saying dont use OOP at all - just be aware that your application will run slower if you're creating lots of objects so bear that in mind when deciding whether to use a function, procedural code or OOP.

            If your script is doing something 1 million times, based on the numbers in that experiment, OOP or functions could mean the difference between 3 minutes and 8 minutes - which is significant - and that was just a very basic example.
            http://www.hazelryan.co.uk

            Comment


            • #7
              PHP 5 objects are a lot faster than PHP 4 ones, and they are passed by handle by default instead of by copy which also helps keeps speed up and memory usage down. Objects always are going to be slower than more traditiional methods of code structuring but the overhead with PHP 5 really isn't all that enormus unless you're running it on a 10 year old server or something. When it comes to execution speed, poor choice of algorithim is going to have far more impact than whether or not you use objects. For example, bubble sort is easy to implement and if fine when working with 5000 data points or less, but beyond that it will really slow your code down regardless of whether it's implemented as a class or a function.

              C++ supports multiple inheritence, a class can be a direct subclass of any number of superclasses. PHP uses a model more like Java, where a subclass can only directly inherit from one superclass and (in PHP 5) can implement any number of interfaces.

              In PHP classes can be inline with your code, stored in a single external file that you can include or there can be one file per class, it's down to personal choice in how you want to organize your code really. Each approach has advantages and disadvantages. All classes in one file means you only have to include that one file, but it also means that Zend has to compile the whole lot into PHP byte code even if you are only going to use one or two of the classes in the file. Putting each class in its own file will overcome this overhead, but it will also mean either lots and lots of include statements in your code or (in PHP 5) you will need to implement an __autoload function.

              Comment


              • #8
                If you create one include to include everything, you're adding a lot of initialization time to your script. If you don't need all of those symbols, you're wasting resources, so only use that global include idea for things you really do use everywhere.

                The difference in speed between OO and procedural is true everywhere, not just PHP, except in the case of the most basic objects in certain languages. What one should be aware of is their algorithmic efficiency, and the cost of each statement. NancyJ makes a good point by ensuring the OP is aware that the cost of creating an object is more than the cost of executing a function, because the OP needs to be aware of the cost of statements to develop algorithms effectively.

                The biggest difference between PHP and C++, however, is that C++ is compiled and PHP is interpreted. If the OP has never used an interpreted language before, I recommend reading through all of the language documentation for PHP (no need to read through all of the library documentation in one shot though). There are LOTS of things that are possible in PHP that are not possible in C++, and there are quite a few constructs in C++ that aren't available in PHP. For example, you cannot overload a function in PHP but you can have optional parameters. I think the biggest thing to note is that String manipulation in PHP is an order(s) of magnitude slower than C and C++ String manipulation. So much so that heavy string manipulation scripts are often re-written in C as PHP modules.

                The other posters have all made valid points on the differences between C++ classes and PHP classes, but I think you're going to be most successful making the transition if you read what is and is not possible in the PHP documentation.

                Comment


                • #9
                  In regards to using a single "global" include file:

                  I personally use this method because I do everything oop and I isolate it in it's own "class.xxx.php" file. Most of these classes rely on one another and are likely to be used by any page load.

                  For my purposes, it's just how i organize my code. I fully agree, if you have a class that is only used in your admin section, only include it in your admin section.

                  Consider having a single "config.global.php" file, which (like the name suggests) includes files and constants that are used globally. And in that "config.global.php" file, have a statement which detects which portion of the site you're in, and include that portion specific code (config.public.php,config.admin.php,etc).

                  Using a method similar to what I described should help keep your code and includes organized as you're still only including "config.global.php" - the global include is just smart enough to know where the user is and what additional code is required.
                  Active PHP/MySQL application developer available for immediate work.
                  syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready

                  Comment

                  Working...
                  X