Professional Web Applications Themes

Class inheritance behaviour - PHP Development

Just double checking with the php crowd. Code example: ------------- <?php $bar = new second_class; echo $bar->foo() . '<br />'; var_dump(get_class_methods('second_class')); class first_class { function foo() { echo 'foo from class first_class'; } } class second_class extends first_class { function foo() { echo 'foo from class second_class'; } } ?> ----------- My goal (if feasable). To be able to house multiple classes under one class, and to not have copied references of one class in another. For example, housing ADODB, Smarty, and other misc classes under one class/object, and not having a copied reference to ADODB in other "child" classes ...

  1. #1

    Default Class inheritance behaviour

    Just double checking with the php crowd.
    Code example:
    -------------
    <?php

    $bar = new second_class;
    echo $bar->foo() . '<br />';
    var_dump(get_class_methods('second_class'));

    class first_class
    {
    function foo()
    {
    echo 'foo from class first_class';
    }
    }

    class second_class extends first_class
    {
    function foo()
    {
    echo 'foo from class second_class';
    }
    }

    ?>
    -----------
    My goal (if feasable). To be able to house multiple classes under one
    class, and to not have copied references of one class in
    another.
    For example, housing ADODB, Smarty, and other misc classes under one
    class/object,
    and not having a copied reference to ADODB in other "child" classes that
    need access to a database.
    Now to my code snippet above.
    What Im testing is to see if class methods behave like regular
    functions, where
    it cannot be defined more than once.
    Trying to define a function more than once produces an error, but it
    seems that redefining a class method overwrites the previous class
    method of the same name.
    So if I were to work towards my goal, there is a possiblity that class
    methods can overwrite themselves.
    So (theoritically) if Smarty and ADODB have the same class method names,
    problems would arise.

    So Im looking to see if this is "normal" class behaviour.
    If so, then I shouldn't even try to work towards my goal, as its not
    prudent.
    Gerard Samuel Guest

  2. #2

    Default Re: [PHP] Class inheritance behaviour

    Method overriding is one of the core principles of any Object Oriented
    (OO) language. By having second_class extend first_class you get all the
    functionality of first_class, but generally you extend a class because
    you want to inherit all of the functionality with a few differences. For
    this reason method overriding is important to get the different
    functionality but maintain the developer interface. So yes the behaviour
    you are asking about is correct. Generally speaking I can't see a
    problem occuring between Smarty and ADODB, since you are extending
    these, and you can currently only extend one or the other. So it follows
    that multiple definitions cannot occur unless you specifically attempt
    to multiply define the same method.

    HTH and HIU (Hope I Understood :)

    Cheers,
    Rob.


    On Sun, 2003-09-21 at 12:41, Gerard Samuel wrote:
    > Just double checking with the php crowd.
    > Code example:
    > -------------
    > <?php
    >
    > $bar = new second_class;
    > echo $bar->foo() . '<br />';
    > var_dump(get_class_methods('second_class'));
    >
    > class first_class
    > {
    > function foo()
    > {
    > echo 'foo from class first_class';
    > }
    > }
    >
    > class second_class extends first_class
    > {
    > function foo()
    > {
    > echo 'foo from class second_class';
    > }
    > }
    >
    > ?>
    > -----------
    > My goal (if feasable). To be able to house multiple classes under one
    > class, and to not have copied references of one class in
    > another.
    > For example, housing ADODB, Smarty, and other misc classes under one
    > class/object,
    > and not having a copied reference to ADODB in other "child" classes that
    > need access to a database.
    > Now to my code snippet above.
    > What Im testing is to see if class methods behave like regular
    > functions, where
    > it cannot be defined more than once.
    > Trying to define a function more than once produces an error, but it
    > seems that redefining a class method overwrites the previous class
    > method of the same name.
    > So if I were to work towards my goal, there is a possiblity that class
    > methods can overwrite themselves.
    > So (theoritically) if Smarty and ADODB have the same class method names,
    > problems would arise.
    >
    > So Im looking to see if this is "normal" class behaviour.
    > If so, then I shouldn't even try to work towards my goal, as its not
    > prudent.
    >
    > --
    > PHP General Mailing List ([url]http://www.php.net/[/url])
    > To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]
    >
    >
    --
    ..------------------------------------------------------------.
    | InterJinn Application Framework - [url]http://www.interjinn.com[/url] |
    :------------------------------------------------------------:
    | An application and templating framework for PHP. Boasting |
    | a powerful, scalable system for accessing system services |
    | such as forms, properties, sessions, and caches. InterJinn |
    | also provides an extremely flexible architecture for |
    | creating re-usable components quickly and easily. |
    `------------------------------------------------------------'
    Robert Cummings Guest

  3. #3

    Default Re: [PHP] Class inheritance behaviour

    Robert mings wrote:
    >Method overriding is one of the core principles of any Object Oriented
    >(OO) language. By having second_class extend first_class you get all the
    >functionality of first_class, but generally you extend a class because
    >you want to inherit all of the functionality with a few differences. For
    >this reason method overriding is important to get the different
    >functionality but maintain the developer interface. So yes the behaviour
    >you are asking about is correct. Generally speaking I can't see a
    >problem occuring between Smarty and ADODB, since you are extending
    >these, and you can currently only extend one or the other. So it follows
    >that multiple definitions cannot occur unless you specifically attempt
    >to multiply define the same method.
    >
    Thanks for replying.
    The idea that I have in mind is something like ->
    1. Create a dummy class with no methods/variables (if neccessary).
    class dummy
    {
    }

    2. Have ADODB extend dummy
    class ADODB extends dummy
    {
    function Execute()
    {
    }
    }

    3. Have Smarty extend dummy
    class Smarty extends dummy
    {
    function display()
    {
    }
    }

    Repeat as neccessary for other known classes, or autoload them from a
    function.
    So if method overwriting is normal behaviour, then the idea I have is
    potentially not sound as it can break interfaces.
    Is it the best way to go, Im not sure at the moment.
    I may just leave my code as it is. Im passing the DB object by
    reference. So what am I worried about....
    Thanks again...
    Gerard Samuel Guest

  4. #4

    Default Re: [PHP] Class inheritance behaviour

    Robert mings wrote:
    >Are you looking to create a single point of access to any libraries your
    >application may wish to use
    >
    Yes. Instead of juggling 5-6 objects, just move around one object.
    > and to subsequently base this on a singleton
    >pattern (only one instance of the object may exist?)?
    >
    Yes. Currently in my code, there is the ADODB object, which is used as
    normal.
    I pass this object by reference to Smarty so its plugins can access the
    DB if neccessary,
    and two other classes that need DB access. Im going on the assumption,
    that
    if one doesn't have to pass by reference, then dont do it, especially
    large objects,
    and having multiple copies of them.

    All of this is just a thought. Whether its feasable, is another story
    for my app....
    Gerard Samuel Guest

Similar Threads

  1. inheritance
    By mr. App in forum ASP.NET Building Controls
    Replies: 1
    Last Post: July 2nd, 10:04 PM
  2. Inheritance in ActionScript
    By JustAskin in forum Macromedia Flash Actionscript
    Replies: 0
    Last Post: February 9th, 03:38 PM
  3. Screen Class Inheritance
    By misterscrub webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 0
    Last Post: January 18th, 08:21 PM
  4. Class inheritance errors
    By Gray Dragon in forum PHP Development
    Replies: 3
    Last Post: September 18th, 02:46 AM
  5. Newbie class inheritance question
    By Jon in forum ASP.NET General
    Replies: 1
    Last Post: August 6th, 08:24 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139