SitePoint Sponsor

User Tag List

Results 1 to 22 of 22

Thread: Classes?

  1. #1
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow Classes?

    Hi everyone,

    I am setting up a small application and am not sure if what I want is possible. Can you have a variable and a class named the same? Eg.

    Class.X = 10;
    Class.X.Get();


    Is there a way to do this that I'm not aware of, or am I looking at this the wrong way? Or do I have to do something like this:

    Class.X.Value = 10;
    Class.X.Get();


    Any help would be greatly appreciated.

    Regards,
    Jordan

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you have a variable and a class named the same? Eg.

    Class.X = 10;
    Class.X.Get();
    Name of class = "Class"
    Name of variable = "X"
    "Class" != "X"

  3. #3
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    Name of class = "Class"
    Name of variable = "X"
    "Class" != "X"
    I understand this I think, but because I am dealing with sub-classes I need to know if I can have the following:

    Name of class = "Class"
    Name of variable in "Class" = "X"
    Name of subclass in "Class" also = "X"

    Possible?

    Regards,
    Jordan

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Name of variable in "Class" = "X"
    Name of subclass in "Class" also = "X"
    How about a concrete example of each and not with a class called 'Class'.

  5. #5
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I want is a way to be able to loop through all possible ways an element can have it's border styled.
    Code:
    Object.Border;                              // '1px solid #fff'
    Object.Border.Set('1px dashed #f00');
    
    Object.Border.Left;                         // '1px solid #fff'
    Object.Border.Left.Set('1px dashed #f00')
    Object.Border.Left.Width;                   // '2px'
    Object.Border.Left.Width.Set('4px');
    I figure if I loop through all possible combinations, I'll have all of the properties at my disposal and I can then work out which ones to use, and which ones not to. I would use some cascading if statements to work out which to apply to my new object. I can't think of a better way to deal with the variety of ways in which an element can have it's border properties applied.
    Code:
    .class
    {
      border: 1px solid #000;
      border-left: 2px solid #000;
      border-right-color: #f00;
    }
    Does what I want make sense? Am I barking up the wrong tree with my solution?

    Regards,
    Jordan

  6. #6
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bump. Anyone got any thoughts on what I'm trying to do here?

    Regards,
    Jordan

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, the whole thread doesn't make any sense.

  8. #8
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmmmm. Sorry, I'll try and post a more sensible case for this one a little later.

    Regards,
    Jordan

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    Post an example that uses a "sub class" that you keep mentioning.

  10. #10
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry it is me confusing terms that is causing so much confusion here. I have found another way around what I was after, however to explain what I originally wanted.

    Is this possible?

    Object.Border.Top = "1px solid #fff";
    Object.Border.Top.Width = "1px";
    Object.Border.Top.Solid = "solid";
    Object.Border.Top.Color = "#fff";

    Can the variable Top contain a value, as well as containg it's own sub-variables?

    Regards,
    Jordan

  11. #11
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What he wants is for an object variable to be 2 different types of variables, one being an Object; say a string and an Object, an integer and an Object, etc...

    The only way that I know of that this is possible in any language is with Object Overloading; that is the variable is just an Object, but under certain conditions you can change its behavior "magically", one possibility being that it returns a value rather than the Object itself.

    And to my knowledge, Javascript is not natively capable of Object Overloading. And I don't know of any Javascript libraries that allow this in some way.

  12. #12
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhhhh, bingo! Thank you dreamscape, that is what I was thinking of I believe, and I believe Java has Object Overloading which is where I'm thinking of it from.

    Regards,
    Jordan

  13. #13
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this possible?
    Code:
    Object.Border.Top = "1px solid #fff";
    Object.Border.Top.Width = "1px";
    How many seconds would that take to test? Here, add this code:
    Code:
    alert(Object.border.Top);
    alert(Object.border.Top.Width);

  14. #14
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Testing the values is obviously easy, I am just not sure on the syntax to build such a structure if it is possible, which it seems like it is not.

  15. #15
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only way that I know of that this is possible in any language is with Object Overloading
    What language has "object overloading"?

  16. #16
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Location
    Lucknow, UP, India
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    What language has "object overloading"?

    What i think here is that he may define the toString method so that using object itself in output methods will print its variables, something like this...
    Code:
    var DIV = {
      Top:{
        Style: 'solid',
        Width: '1px',
        Color: '#ff0',
        toString: function() {
          return this.Style + ' ' + this.Width + ' ' + this.Color;
        }
      }
    }
    alert(DIV.Top);
    alert(DIV.Top.Style);
    this snip gives just the result he wants
    Who the hell is "General Failure"
    and why is he reading my hard disk???

  17. #17
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    What language has "object overloading"?
    A number of dynamically typed languages have object overloading. PHP 4 to some extend since 4.3 with an experimental extension, and PHP 5 has native object overloading, with 5.1 having significant improvements over 5.0.x. I think ruby also supports object overloading to some extent. Python has object overloading.

    Statically typed languages can also have object overloading, though it is a bit different than dynamic languages. I believe C++ has object overloading. And Java has what it calls "method overloading".

    I'm not familiar with all that many languages (and really only know & use PHP & Javascript), but I don't doubt that there are many other languages that support object overloading in one way or another.

  18. #18
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Any proper object oriented language has object overloading as it is part of what it has to have to properly support object oriented programming.

    While Javascript supports much of OO overloading is one of the parts that it doesn't support.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  19. #19
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Python has object overloading.
    I couldn't find anything on google for object overloading in Python.

    Any proper object oriented language has object overloading as it is part of what it has to have to properly support object oriented programming.
    Would you consider C++ or Java a "proper object oriented language"? A google search doesn't turn up anything for object overloading in either C++ or Java.

    A number of dynamically typed languages have object overloading. PHP 4 to some extend since 4.3 with an experimental extension, and PHP 5 has native object overloading, with 5.1 having significant improvements over 5.0.x.
    Php is the only language that google identifies as having something called object overloading, and it's new in php5. Furthermore, from reading the docs, as far as I can tell, all that the "object overloading" feature in PHP does is allow you to call some default functions or access some default values when you call a function or try to access a property that doesn't exist in the class. It does not allow variables in a class to somehow be assigned two different values.
    Last edited by 7stud; Mar 18, 2006 at 17:25.

  20. #20
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    It does not allow variables in a class to somehow be assigned two different values.
    I never said that it did.

  21. #21
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    C++ definitely has overloading as does any other proper object oriented language since that is part of the requirements for a language to be completely object oriented.

    PHP has moved much closer to being a proper object oriented language with version 5 compared to version 4. It is expected that version 6 will be a proper OO language.

    Javascript version 1 supports some of the OO constructs but does not even support defining classes properly. Javascript 2.0 adds proper support for classes but I am not sure if that extra support includes overloading. JS 2.0 is not supported client side. You can use the Microsoft equivalent to JS 2.0 in .NET.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  22. #22
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall
    Any proper object oriented language has object overloading
    Quote Originally Posted by felgall
    C++ definitely has overloading
    ...


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
  •