SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast minus's Avatar
    Join Date
    Jan 2002
    Location
    Pittsburgh, PA, USA
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ?? $a=5 -> $this->a=5 ??

    Baffling title isn't?

    I m looking for an object method that would transform the argument(s) (scalar, array, whatever..) passed to it into attribute(s) of this object BY KEEPING the SAME name (and same value of course...).

    Thus, the variable $minus (value=78) would be passed to a method that would make $this->minus (value=78) available.

    Let me know if that wasn't clear enough.

    I've already spent some time on this and couldn't figure it out. The one that solves this gains my respect for life! Not gonna feed you but still...

  2. #2
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this should be what you want
    PHP Code:
    class MyClass {
        var 
    $minus;

        function 
    setMinus ($minus) {
            
    $this->minus $minus;
        }
    }
    $class = new MyClass;
    $class->setMinus($minus); 
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  3. #3
    SitePoint Enthusiast minus's Avatar
    Join Date
    Jan 2002
    Location
    Pittsburgh, PA, USA
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Indeed, that will work if you already know the name of the variable; therefore, you can hard-code it in the method. However, in my problem, you do not know the name of the variable until you pass it to the method. Here is the trick: you got to extract the name of the variable!

    Also, I did put '(s)' at the end of argument(s). I meant that it should be able to handle more than one (10, 20 maybe) arguments.

    Sorry, I haven't enough defined my problem in the first place. Can you see the complexity of the thing now?

  4. #4
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is not something meant for OOP. Good OOP code will usually not take any input from the user or will only use it in the way I have shown. To do what you ask you would have to have a script rewrite the class each time to add another var $var; which would not work if it was dynamic.

    So basically, you're screwed. :P
    j/k
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  5. #5
    SitePoint Enthusiast minus's Avatar
    Join Date
    Jan 2002
    Location
    Pittsburgh, PA, USA
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why does it have to be from the user? In my code it isn't...

    Anyways.

    Someone else maybe?

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    Canada
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't this defeat the purpose of OOP ? Objects cannot dynamically alter their attributes. Sure the values of the attributes can change but I didn't think you could change the number of attributes in some instance. At least that doesn't make sense to me.

    Kix.

  7. #7
    SitePoint Enthusiast minus's Avatar
    Join Date
    Jan 2002
    Location
    Pittsburgh, PA, USA
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK OK. I m wrong.

    Still, I m calling a method from outside the object, passing it some arguments that become attributes. Of course, I m supposed to know how many arguments I pass to it; but a method would reduce the code needed to have these arguments as attributes. I m sorry you can't see that.

    I guess you can't solve my problem either. Thanks anyway.


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
  •