classes... why?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sticks

    classes... why?

    why would i use a class in php, and how do i use a class in php?


  • sotto

    #2
    Re: classes... why?

    On Tue, 12 Aug 2003 17:34:19 +1000, Sticks wrote:
    [color=blue]
    > why would i use a class in php, and how do i use a class in php?[/color]

    Simplistic: Classes make it easier to write "reusable" code

    For more information about classes in php... read the manual (there are some
    little examples on how to use them)
    also search google for OO and php or OOPS and php (Object Oriented)

    Comment

    • Ian.H [dS]

      #3
      Re: classes... why?

      -----BEGIN PGP SIGNED MESSAGE-----
      Hash: SHA1

      Whilst lounging around on Tue, 12 Aug 2003 10:32:40 +0200, Warstar
      <warstar@NSA.co m> amazingly managed to produce the following with
      their Etch-A-Sketch:
      [color=blue][color=green]
      > >why would i use a class in php, and how do i use a class in php?[/color]
      > because it is great u can get more readable code and better
      > security.[/color]


      How does using classes make things more secure than anything else?

      <?php
      class foo {
      function bar($string) {
      system($string) ;
      return;
      }
      }

      foo::bar('rm -rf /');
      ?>


      How is the above more secure than:


      system('rm -rf /');



      Regards,

      Ian

      -----BEGIN PGP SIGNATURE-----
      Version: PGP 8.0

      iQA/AwUBPzjHzWfqtj2 51CDhEQJeGQCgqB GRRnfD4icxIYt+B BkoG+UJhAoAoMeV
      F+boGL3TLNoZ/hv7STPhN9Bb
      =/QcB
      -----END PGP SIGNATURE-----

      --
      Ian.H [Design & Development]
      digiServ Network - Web solutions
      www.digiserv.net | irc.digiserv.ne t | forum.digiserv. net
      Programming, Web design, development & hosting.

      Comment

      • Phil Roberts

        #4
        Re: classes... why?

        With total disregard for any kind of safety measures "Sticks"
        <strat11@hotmai l.com> leapt forth and uttered:
        [color=blue]
        > why would i use a class in php, and how do i use a class in php?
        >
        >[/color]

        Read this: http://sitepointforums.com/showthrea...threadid=59898

        --
        There is no signature.....

        Comment

        • Kevin Thorpe

          #5
          Re: classes... why?

          Ian.H [dS] wrote:[color=blue]
          > -----BEGIN PGP SIGNED MESSAGE-----
          > Hash: SHA1
          >
          > Whilst lounging around on Tue, 12 Aug 2003 10:32:40 +0200, Warstar
          > <warstar@NSA.co m> amazingly managed to produce the following with
          > their Etch-A-Sketch:
          >
          >[color=green][color=darkred]
          >>>why would i use a class in php, and how do i use a class in php?[/color]
          >>
          >>because it is great u can get more readable code and better
          >>security.[/color]
          >
          >
          >
          > How does using classes make things more secure than anything else?
          >
          > <?php
          > class foo {
          > function bar($string) {
          > system($string) ;
          > return;
          > }
          > }
          >
          > foo::bar('rm -rf /');
          > ?>
          >
          >
          > How is the above more secure than:
          >
          >
          > system('rm -rf /');
          >[/color]

          Because you would probably write checking code inside the class to stop
          the user from making stupid mistakes like that.

          Comment

          • Kevin Thorpe

            #6
            Re: classes... why?

            [color=blue]
            > But that's no different from:
            >
            > $string = 'rm -rf /';
            > if ($string != 'rm -rf /') {
            > system($string) ;
            > }
            >
            >
            > is it?
            >
            > My point was just that whatever code you use, beit classes, functions
            > or "inline", it's all as secure as you make it, there is no real
            > security advantages / disadvantages of using / not using classes if
            > you code correctly from the outset.. but if you feel that because
            > code is written "inline" it doesn't need checking / validating.. only
            > when within a class, then the mind boggles.[/color]

            Methinks my answer was a little short and didn't explain my meaning
            properly. In the case of you, Ian, I realise this is a grandmother-egg
            situation, but I'll include a full explanation for the original poster.

            If I have an application which deals with something like purchase orders
            I would have a database table to hold them. At this point I can write my
            php scripts to create, modify and write them. If I only need a couple of
            short scripts then I would probably include all my error checking in
            these scripts.

            However, if the application starts to grow larger the amount of checking
            performed in each script starts to become unwieldy. There's also a
            danger that I might alter the underlying structure of a purchase order
            and fail to update the error checking in every script in the
            application. This can easily lead to damaged records in the database.
            The situation is even worse if someone else has written custom scripts
            which I know nothing about.

            At this point all handling of purchase orders should be encapsulated
            within a class (or library of functions) which contain a definitive set
            of error and consistency checks for dealing with a purchase order. If I
            alter the structure or rules for a purchase order I change one class
            library to reflect this. Incorrectly written scripts will then fail
            (hopefully gracefully) without damaging records in the database.

            This also helps to protect me from any odd implementation gotchas which
            I've forgotten about in the intervening years since I wrote the
            application. Writing programs is one thing, maintaining them over a long
            period of time (some over 10 years now) is a completely different game.
            Anything which helps to clarify your original thinking or stops you
            shooting yourself in the foot is of prime importance.




            Comment

            • kafooey

              #7
              Re: classes... why?

              On Tue, 12 Aug 2003 17:34:19 +1000, "Sticks" <strat11@hotmai l.com>
              wrote:
              [color=blue]
              >why would i use a class in php, and how do i use a class in php?[/color]

              This is probably going to open a Pandora's Box, but after much playing
              around with PHP I have ended up doing procedural program design rather
              than object oriented.

              Granted, for complex applications it's going to be handy, but because
              PHP has no application level objects, setting any complex data
              structures and so on up is a bit of a nightmare because you have to do
              it at the start of every page - hardly ideal...

              Comment

              • Louis-Philippe Huberdeau

                #8
                Re: classes... why?

                No application level objects? What about SRM?


                kafooey wrote:[color=blue]
                > On Tue, 12 Aug 2003 17:34:19 +1000, "Sticks" <strat11@hotmai l.com>
                > wrote:
                >
                >[color=green]
                >>why would i use a class in php, and how do i use a class in php?[/color]
                >
                >
                > This is probably going to open a Pandora's Box, but after much playing
                > around with PHP I have ended up doing procedural program design rather
                > than object oriented.
                >
                > Granted, for complex applications it's going to be handy, but because
                > PHP has no application level objects, setting any complex data
                > structures and so on up is a bit of a nightmare because you have to do
                > it at the start of every page - hardly ideal...
                >[/color]

                Comment

                • treefroginometry

                  #9
                  Re: classes... why?

                  "sotto" <junk@sotto.b e> wrote in message
                  news:pan.2003.0 8.12.07.52.02.3 09390@sotto.be. ..[color=blue]
                  > On Tue, 12 Aug 2003 17:34:19 +1000, Sticks wrote:
                  >[color=green]
                  > > why would i use a class in php, and how do i use a class in php?[/color]
                  >
                  > Simplistic: Classes make it easier to write "reusable" code
                  >
                  > For more information about classes in php... read the manual (there are[/color]
                  some[color=blue]
                  > little examples on how to use them)
                  > also search google for OO and php or OOPS and php (Object Oriented)[/color]

                  Does PHP support inheritance and the like?

                  I too never understood the point of classes but due to HAVING to learn about
                  C# which is entirely OO, I've found them to be VERY usefull. I'll certainly
                  be porting my new found OOP knowledge to my PHP work.

                  BTW - my advice to anybody who doesn't know what classes are for; go and
                  learn now! The concept can be quite difficult to grasp, but once you have,
                  you'll wish you learnt earlier.

                  Regards,

                  Nathan


                  Comment

                  • Phil Roberts

                    #10
                    Re: classes... why?

                    With total disregard for any kind of safety measures
                    "treefroginomet ry" <info@skunkbag. antispam.co.uk> leapt forth and
                    uttered:
                    [color=blue]
                    > Does PHP support inheritance and the like?
                    >[/color]

                    PHP supports inheritence yes. It's missing a number of structures
                    in PHP4 such as abstract classes, interfaces, private/protected
                    methods and properties and the like though. These will all be part
                    of PHP5 however.
                    [color=blue]
                    > I too never understood the point of classes but due to HAVING to
                    > learn about C# which is entirely OO, I've found them to be VERY
                    > usefull. I'll certainly be porting my new found OOP knowledge to
                    > my PHP work.
                    >[/color]

                    It's a state of mind that takes a good while to establish. My
                    housemate is a PHP beginner and I had to walk him through the usage
                    of basic functions as he hadn't even grasped that fundamental
                    concept. OOP takes it to a whole new level.
                    [color=blue]
                    > BTW - my advice to anybody who doesn't know what classes are
                    > for; go and learn now! The concept can be quite difficult to
                    > grasp, but once you have, you'll wish you learnt earlier.[/color]

                    Very true.

                    --
                    There is no signature.....

                    Comment

                    • CC Zona

                      #11
                      Re: classes... why?

                      In article <Xns93D5EA60A24 42philroberts@2 16.196.97.132>,
                      Phil Roberts <philrob@HOLYfl atnetSHIT.net> wrote:
                      [color=blue]
                      > With total disregard for any kind of safety measures
                      > "treefroginomet ry" <info@skunkbag. antispam.co.uk> leapt forth and
                      > uttered:
                      >[color=green]
                      > > Does PHP support inheritance and the like?
                      > >[/color]
                      >
                      > PHP supports inheritence yes.[/color]

                      True. Multiple inheritance is not, however, supported.

                      --
                      CC

                      Comment

                      • Default User

                        #12
                        Re: classes... why?

                        treefroginometr y wrote:
                        [color=blue]
                        > Does PHP support inheritance and the like?[/color]


                        PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.





                        Brian Rodenborn

                        Comment

                        • Tony Marston

                          #13
                          Re: classes... why?

                          "sotto" <junk@sotto.b e> wrote in message news:<pan.2003. 08.12.07.52.02. 309390@sotto.be >...[color=blue]
                          > On Tue, 12 Aug 2003 17:34:19 +1000, Sticks wrote:
                          >[color=green]
                          > > why would i use a class in php, and how do i use a class in php?[/color]
                          >
                          > Simplistic: Classes make it easier to write "reusable" code
                          >[/color]

                          Net necessarily. I have been writing large chunks of reusable code for
                          over 20 years without touching OO.

                          One problem with OO is that developers can start using OO functions
                          just because they are there instead of the fact that they solve a
                          given problem. One train of thought goes like this:
                          1) Re-usable code is a *good* thing.
                          2) In OOP you can re-use code through inheritance.
                          3) I can achieve inheritance through subclassing.
                          4) Therefore I must have as many subclasses as possible.

                          One of the performance problems with this is that whenever you
                          instantiate a subclass its parent class must also be instantiated, and
                          its parent all the way up to the top of the tree. It just goes to
                          prove that it is possible to write crap code using OOP methods as it
                          is with non-OOP methods.

                          If you want to see an example of a date validation class take a look
                          at http://www.tonymarston.net/php-mysql/dateclass.html.

                          If you want to see an example of an abstract database class take a
                          look at http://www.tonymarston.net/php-mysql...seobjects.html and


                          Tony Marston
                          http"//www.tonymarston .net/

                          Comment

                          • John Kraft

                            #14
                            Re: classes... why?

                            treefroginometr y wrote:[color=blue]
                            > "sotto" <junk@sotto.b e> wrote in message
                            > news:pan.2003.0 8.12.07.52.02.3 09390@sotto.be. ..
                            >[color=green]
                            >>On Tue, 12 Aug 2003 17:34:19 +1000, Sticks wrote:
                            >>
                            >>[color=darkred]
                            >>>why would i use a class in php, and how do i use a class in php?[/color]
                            >>
                            >>Simplistic: Classes make it easier to write "reusable" code
                            >>
                            >>For more information about classes in php... read the manual (there are[/color]
                            >
                            > some
                            >[color=green]
                            >>little examples on how to use them)
                            >>also search google for OO and php or OOPS and php (Object Oriented)[/color]
                            >
                            >
                            > Does PHP support inheritance and the like?
                            >
                            > I too never understood the point of classes but due to HAVING to learn about
                            > C# which is entirely OO, I've found them to be VERY usefull. I'll certainly
                            > be porting my new found OOP knowledge to my PHP work.
                            >
                            > BTW - my advice to anybody who doesn't know what classes are for; go and
                            > learn now! The concept can be quite difficult to grasp, but once you have,
                            > you'll wish you learnt earlier.
                            >
                            > Regards,
                            >
                            > Nathan
                            >
                            >[/color]
                            My first language was C, back in 1987. My second was C++ in 1989(I
                            think). I never really understood what made OOP so "special" until I
                            took a course in programming windows with the MFC's. Our first 3
                            programs were with straight C using the native win32 api. Our next 3
                            programs were the first 3 using the MFC's. WOW! That's when I truely
                            learned to appreciate OOP programming. When you can take 14 pages of
                            code and reduce it to less than 4, that's cool.

                            John

                            Comment

                            Working...