maybe a stupid question regarding classes

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

  • Alice
    Guest replied
    Re: maybe a stupid question regarding classes


    "chris" <someone@here.c om> wrote in message
    news:3fd89047$1 @funnel.arach.n et.au...[color=blue]
    > What is a class ?? is it like a function ??[/color]

    A function is a collection of statements.
    When a function is "called" by program code it can be fed data (the function
    parameters) which the statements will process to "return" a result, the
    function's return value.

    A class is a collection of variables together with some functions which set
    the values of those variables.
    Classes are an integral part of "Object Oriented Programming".
    The Basic of the '80s was NOT object oriented.

    A class always has at least one function called its "constructo r".
    Calling this function results in the formation of one "instance" of the
    class.
    A class instance is known as an "object".

    To work with an object you need to obtain a block of memory to store it in.
    You do this by using the "new" operator.
    This operator returns a pointer to the block of memory where the object will
    be stored

    e.g. in javascript there is a class called an array.
    I would guess the array class consists of the constructor function plus a
    dynamic array variable.
    The constructor function, called Array(), returns an object which consists
    of an array the contents of which are determined by the parameters fed to
    the constructor.
    Calling the constructor might look like this:
    weekday=new Array("Sun","Mo n","Tue","Wed", "Thu","Fri","Sa t");

    weekday is an object that consists of an array of the days of the week.

    One book I have read likened an object to a cookie (real edible cookies -
    not the kind you find on the web) and a class to a cookie cutter. The
    definition of the class determines the shape of the object.

    The PHP manual describes it thus:
    "Classes are types, that is, they are blueprints for actual variables. You
    have to create a variable of the desired type with the new operator. "

    Check it out in the manual for yourself



    HTH
    Orson








    Leave a comment:


  • Bruno Desthuilliers
    Guest replied
    Re: maybe a stupid question regarding classes

    chris wrote:[color=blue]
    > What is a class ?? is it like a function ??
    >
    > this has allways confused me as i am a newby to programming (since Basic in
    > the 80's)
    >
    > thanks for any insight you can give
    >
    >[/color]

    OOP-101 :

    Imagine that you have to work with a bunch of data.

    Imagine that these data are organised in the same way, let's say that
    each data item is made of a "name" field, a "descriptio n" field, a
    "price" field etc..., all havings the same fields.

    One obvious way to organise you data is to have each 'item' (one "name"
    field + one "descriptio n" field + one "price" field etc) being an
    associative array :

    mydata1 = ['name' => 'yoyo',
    'description' => 'A nice, fluo, glowing yoyo',
    'price' => 1.49];

    mydata2 = ['name' => 'PHP T-Shirt',
    'description' => 'A nice, fluo PHP T shirt',
    'price' => 9.49];


    now you can use mydata1 or maydata2 etc a whole thing, and still
    retrieve your datas...

    Ok, now you're there, you may want to *do* things with your data, like,
    say, displaying'em in a nice, fluo (err... sorry) web page. You could of
    course code this data item by data item, but since *all* your items have
    the same fields, it would be more simple to just write one function :

    function printItem($item ) {
    $buf = "Buy now our wonderful "
    . <i>" . $item["name"] . "</i> : "
    . $item["descriptio n"]
    . " for just "
    . " <b>" . $item["price"] . "</b><br>\n"
    echo $buf;
    }


    printItem(mydat a1);
    printItem(mydat a2);

    But you don't just have to print your items, you have a lot of things to
    do with them : reading them from a [DBMS|XML file|csv file|...], storing
    them to a..., etc.

    So you have a whole lot of 'doSomethingWit hMyItem' functions to write.

    So far so good, you've defined what you could call an 'abstract data
    type' - there is much more than this to ADTs, but well, you've got the
    point.

    Now imagine that instead of having your 'data type' (the associative
    array) and a bunch of functions taking one of those 'data type' item as
    an arg, you just add the functions *into* (well, that's not how it
    works, but that's how it looks like) the associative array, and give a
    name - say 'Item' - to that data type. Now you could for exemple rewrite
    your code like :

    class Item {
    $var name;
    $var description;
    $var price;

    // this function lets you initialize an Item
    // when you 'create' it.
    // Note that the '$this' thing is a reference to
    // the specific Item that you're working with
    function Item($name, $description, $price) {
    $this->name = $name;
    $this->description = $description;
    $this->price = $price;
    }

    // Note how the '$item' param in the previous version
    // is now replaces with the '$this' reference
    function print() {
    $buf = "Buy now our wonderful "
    . <i>" . $this->name . "</i> : "
    . $this->description
    . " for just "
    . " <b>" . $this->price . "</b><br>\n"
    echo $buf;
    }
    }

    mydata1 = new Item('yoyo', 'A nice, fluo, glowing yoyo', 1.49);
    mydata2 = new Item('PHP T-Shirt', 'A nice, fluo PHP T shirt', 9.49);

    // NB : the 'print' operation has been defined
    mydata1->print();
    mydata2->print();

    Well, basically, that's just what it is.

    A class is a way to define a named data type, with a data structure and
    some operations working on that data structure.

    Then you can create 'objects' (or 'instances') of that type, and call
    operations on them.

    Now there is more than this in classes and objects, but I'll let you
    discover what... !-)

    HTH
    Bruno

    Leave a comment:


  • Jedi121
    Guest replied
    Re: maybe a stupid question regarding classes

    "NK" a écrit le 11/12/2003 :[color=blue]
    > I loved BASIC :)
    >
    > Check out this tute, I found it very helpful, written in an easy to
    > understand manner and it will make a LOT more sense of what a class is :)
    >
    > http://codewalkers.com/tutorials/54/1/html[/color]

    Actual link is :
    Learn the PHP object-oriented programming shape that still matters in 2026: classes, methods, constructors, repositories, and the limits of inheritance.


    --
    Have you read the manual?


    Leave a comment:


  • Nikolai Chuvakhin
    Guest replied
    Re: maybe a stupid question regarding classes

    "chris" <someone@here.c om> wrote in message
    news:<3fd89047$ 1@funnel.arach. net.au>...[color=blue]
    >
    > What is a class ?? is it like a function ??[/color]

    The PHP Manual says:

    A class is a collection of variables and functions
    working with these variables.



    You can also think of a class as a comlpex type, something
    like an array, which, in addition to variables, may include
    functions.

    Cheers,
    NC

    Leave a comment:


  • NK
    Guest replied
    Re: maybe a stupid question regarding classes

    I loved BASIC :)

    Check out this tute, I found it very helpful, written in an easy to
    understand manner and it will make a LOT more sense of what a class is :)

    Ordered programming learning paths for PHP, SQL, APIs, browser projects, and future language tracks.


    Cheers,
    NK



    chris wrote:
    [color=blue]
    > What is a class ?? is it like a function ??
    >
    > this has allways confused me as i am a newby to programming (since Basic in
    > the 80's)
    >
    > thanks for any insight you can give
    >
    >[/color]

    Leave a comment:


  • chris
    Guest started a topic maybe a stupid question regarding classes

    maybe a stupid question regarding classes

    What is a class ?? is it like a function ??

    this has allways confused me as i am a newby to programming (since Basic in
    the 80's)

    thanks for any insight you can give


Working...