Classes and funtions

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

    Classes and funtions

    Hi All

    I'm pretty new to PHP. I've done some relatively complicated bits now:
    written my own little shopping cart, written a content management system
    with file uploads, built in newsletters, etc, etc.

    The problem is, my code is probably not very elegant. For example, in my
    CMS I have a page for adding pages and a page for editing pages. Much of
    the code is repeated in the two pages.

    So, what I'm having difficulty with is what's the difference bewteen a class
    and a function? A function to me looks like a piece of code in an external
    file (possibly) that I can call when I need it instead of duplicating code
    in different files. A class looks to achieve the same thing. Am I missing
    the point somewhere?

    Cheers

    Andy


  • Doug

    #2
    Re: Classes and funtions


    Check this out:



    AJ wrote:
    [color=blue]
    > Hi All
    >
    > I'm pretty new to PHP. I've done some relatively complicated bits now:
    > written my own little shopping cart, written a content management system
    > with file uploads, built in newsletters, etc, etc.
    >
    > The problem is, my code is probably not very elegant. For example, in my
    > CMS I have a page for adding pages and a page for editing pages. Much of
    > the code is repeated in the two pages.
    >
    > So, what I'm having difficulty with is what's the difference bewteen a class
    > and a function? A function to me looks like a piece of code in an external
    > file (possibly) that I can call when I need it instead of duplicating code
    > in different files. A class looks to achieve the same thing. Am I missing
    > the point somewhere?
    >
    > Cheers
    >
    > Andy
    >
    >[/color]

    Comment

    • Fabrizio Giammatteo

      #3
      Re: Classes and funtions

      Hi Andy,
      a class is an abstraction that tries to model the real world, building
      objects similar to the real ones.

      For example you could think to a car like an object. The car has some
      attributes: color, model, size, etc...

      and some functions: you can open the door, you can drive, you can brake,
      etc...

      Now imagine that you are building a software and you need to represent a
      car.

      An object oriented way to do this could be (in a pseudolanguage) :

      public class Car {

      private color;
      private model;

      public getColor() {
      return color;
      }

      public setColor(color) {
      this.color = color;
      }

      ...
      }

      that is nothing more than a piece of code, you might write the same
      functions i a procedural language, in the same file o in more than one file.
      The important thing is:

      whit the object oriented programming you can create objects and after you
      can create other objects which inherits their characteristics . In other
      words, you can build things without reinvent the wheel each time.

      I'm not a fanatic of the OOP, sometimes it works sometimes not, it depends
      on your needs and on your project.

      I know I've been a bit confused, but in a little space and not in my mother
      tongue language is very difficult :)))

      Bye
      Fabrizio

      "AJ" <nospam@redcatm edia.net> ha scritto nel messaggio
      news:ce2tl3$29u $1@sparta.btint ernet.com...[color=blue]
      > Hi All
      >
      > I'm pretty new to PHP. I've done some relatively complicated bits now:
      > written my own little shopping cart, written a content management system
      > with file uploads, built in newsletters, etc, etc.
      >
      > The problem is, my code is probably not very elegant. For example, in my
      > CMS I have a page for adding pages and a page for editing pages. Much of
      > the code is repeated in the two pages.
      >
      > So, what I'm having difficulty with is what's the difference bewteen a[/color]
      class[color=blue]
      > and a function? A function to me looks like a piece of code in an[/color]
      external[color=blue]
      > file (possibly) that I can call when I need it instead of duplicating code
      > in different files. A class looks to achieve the same thing. Am I[/color]
      missing[color=blue]
      > the point somewhere?
      >
      > Cheers
      >
      > Andy
      >
      >[/color]


      Comment

      • CJ Llewellyn

        #4
        Re: Classes and funtions

        "AJ" <nospam@redcatm edia.net> wrote in message
        news:ce2tl3$29u $1@sparta.btint ernet.com...[color=blue]
        > Hi All
        >
        > I'm pretty new to PHP. I've done some relatively complicated bits now:
        > written my own little shopping cart, written a content management system
        > with file uploads, built in newsletters, etc, etc.
        >
        > The problem is, my code is probably not very elegant. For example, in my
        > CMS I have a page for adding pages and a page for editing pages. Much of
        > the code is repeated in the two pages.
        >
        > So, what I'm having difficulty with is what's the difference bewteen a[/color]
        class[color=blue]
        > and a function? A function to me looks like a piece of code in an[/color]
        external[color=blue]
        > file (possibly) that I can call when I need it instead of duplicating code
        > in different files. A class looks to achieve the same thing. Am I[/color]
        missing[color=blue]
        > the point somewhere?[/color]

        Yes, a class is designed to "encapsulat e" data and code into one structure
        so you as a programmer don't have to worry about how it works inside, but
        how to use it from the outside.

        For example

        class foo {
        var $a;

        function foo($default) {
        $this->a = $default;
        }

        function inc($amount) {
        $this->a = $this->a + $amount;
        }

        function getValue() {
        return $this->a;
        }
        }

        $oFoo = new foo(0);
        $oFoo->inc(10);
        echo $oFoo->getValue();

        Or somebody else may design foo like this:-

        class foo {
        var $a;

        function foo($default) {
        $this->a = $default;
        }

        function inc($amount) {
        $this->a += $amount;
        }

        function getValue() {
        return $this->a;
        }
        }

        How the function inc works is not important, you just need to know how to
        call it, and it will manipulate the data without you knowing how it does it,
        or where it is storing the data.

        With procedural code you'd probably write:-

        function inc($value , $amount) {
        return $value + $amount;
        }

        $value = 0;
        $vaule = inc($value , 5);
        echo inc($value , 5);
        echo inc($value , 5);

        $value is only retained should you assign it to a variable. In the class
        structure the is retained until the class is destroyed or manipulated again.

        Your inital problem is not to learn how OOP works but to learn how to
        rationalise code so that you are not writing the same code twice. Learning
        OOP is a good idea(tm).


        Comment

        • rush

          #5
          Re: Classes and funtions

          "AJ" <nospam@redcatm edia.net> wrote in message
          news:ce2tl3$29u $1@sparta.btint ernet.com...[color=blue]
          > So, what I'm having difficulty with is what's the difference bewteen a[/color]
          class[color=blue]
          > and a function? A function to me looks like a piece of code in an[/color]
          external[color=blue]
          > file (possibly) that I can call when I need it instead of duplicating code
          > in different files. A class looks to achieve the same thing. Am I[/color]
          missing[color=blue]
          > the point somewhere?[/color]

          Well you would really need to read some stuff on OO programming. But let's
          say that (if applied properly), OO could give you more reuse than you could
          achieve with just functions. For instance, you could have CMSItemForm class,
          which would handle all general form handling in your cms, and 2 subclasses
          of it CMSNewsForm and CMSArticleForm, which would handle only parts specific
          to news or article form, and everything else would be inherited from
          CMSItemForm.

          rush
          --
          Get your very own domain easily. Fast and professional customer service.



          Comment

          • Ben Cottrell

            #6
            Re: Classes and funtions

            AJ wrote:
            [color=blue]
            > So, what I'm having difficulty with is what's the difference bewteen a class
            > and a function? A function to me looks like a piece of code in an external
            > file (possibly) that I can call when I need it instead of duplicating code
            > in different files. A class looks to achieve the same thing. Am I missing
            > the point somewhere?[/color]

            You seem to be close with your definition of a function.

            a function is a 'named' chunk of code.. it's called a function because
            it *does something*.

            (If you're unsure of the concept - think of it in real-world terms,
            similar to the english dictionary definition of 'function'.. for
            example, a function of your wristwatch is that it tells you the time...
            another function of your wristwatch may be that it sounds an alarm.)

            when you use a function by calling it's 'name' (known as "calling a
            function") it executes the chunk of code.

            as you guessed, a function makes this single chunk of code reusable
            anywhere else in your code.



            On the other hand, a Class is a completely different concept... In fact,
            it's a horrible concept to try and understand what it is at first, and
            even worse, try to understand *why* it's useful... But i'll try to
            explain :-)

            So.. first off, Forget programming for a second.. forget your project
            and your code.. the best way (IMHO) to try and understand what a class
            is, is to think about it in real-world terms.

            Think of a "class" as a "blueprint" . (for example, new houses on a
            housing estate have a single blueprint that is used for all houses)..
            for the english dictionary definition, it relates closely to
            'classification '

            a "class" is a "blueprint" for an object... in the same way that a
            blueprint may be for a new house on an estate... Hopefully you
            understand what an object is, else this may be a little confusing
            already :-)

            And see all those new houses on the housing estate.. They're all built
            to the same specification (as per the blueprints)... The specification
            of each of these houses includes properties/attributes (such as Colour
            of front door, garden plants, etc ).. and some Functions (such as "turn
            on the tap", "open garage door", "open the window")... but of course
            these functions can only be performed on the specific house (I mean, you
            don't turn on your tap and run your neighbor's bath do you?)

            When all these houses are first built, they're all pretty much
            identical, however, they are seperate houses which exist on their own
            independently of each other - the only thing that links them is their
            original specification/blueprints... Of course, when people move in,
            they can be repainted, have different plants in the garden.. etc.

            In the same light, all your objects which are constructed from your
            class have the same properties/attributes (Data variables), and also
            contain a bunch of functions that can be performed on the object
            (usually to modify the data variables of the object).

            For example. your class may be called RollOverHyperLi nk .. The
            attributes being maybe IdlePicture, ActivePicture, height, width,
            border, linkURL, alt-text etc.. you get the idea.. Functions could be
            Set_To_Active, Set_To_Inactive .

            Then you could have loads of buttons on your webpage that are Rollover
            hyperlinks which are new objects based on the class RollOverHyperLi nk ..
            each of them given unique properties which are appropriate to every
            specific object (or 'instance').. one may have IdlePicture set to
            billgatescat.jp g and linkURL set to www.microsoft.com, and another may
            have IdlePicture set to Tonyblairinstoc kings.jpg with linkURL set to
            www.number-10.gov.uk ... etc.. Hopefully you get the idea. :-)

            --
            Ben Cottrell AKA Bench

            Disclaimer:
            This post may contain explicit depictions of things which are "real".
            These "real" things are commonly known as 'life'! So, if it sounds
            sarcastic, don't take it seriously. If it sounds hazardous, Do not try
            this at home or at all. And if it offends you, just don't read it.

            Comment

            • Michael Fesser

              #7
              Re: Classes and funtions

              .oO(Ben Cottrell)
              [color=blue]
              >[...]billgatescat.jp g [...][/color]

              Hmm, how would you hyphenate this? ;)

              SCNR
              Micha

              Comment

              • Alex Hunsley

                #8
                Re: Classes and funtions

                Michael Fesser wrote:[color=blue]
                > .oO(Ben Cottrell)
                >
                >[color=green]
                >>[...]billgatescat.jp g [...][/color]
                >
                >
                > Hmm, how would you hyphenate this? ;)
                >
                > SCNR
                > Micha[/color]

                Speaking of which, look at this *terrible* URL for a university:



                (I'm not kidding! It's a university in california! How in their maddest moments
                could they not look at that url and say 'err, it will read strangely to someone
                who has never heard of nippissing!')

                Mysteriously, they've changing the URL to www.nipissingu.ca - I wonder why? :)

                also see:




                (both of which are safe for work and not rude in any way)

                etc. etc.

                Comment

                • Geoff Berrow

                  #9
                  Re: Classes and funtions

                  I noticed that Message-ID: <10gcag586ga9a1 2@corp.supernew s.com> from
                  Alex Hunsley contained the following:
                  [color=blue]
                  >also see:
                  >
                  >www.whorepresents.com
                  >www.powergenitalia.com[/color]

                  also www.cumstore.co.uk
                  and www.antiquesexchange.com

                  both equally safe for work/home/family etc.

                  --
                  Geoff Berrow (put thecat out to email)
                  It's only Usenet, no one dies.
                  My opinions, not the committee's, mine.
                  Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                  Comment

                  Working...