From: Arlo L. <ar...@ar...> - 2009-09-02 08:15:42
|
Hi folks, I guess my last question stumped you! Or confused you. Or you're all on vacation this week. Anyway, here's another one. Does anyone think this kind of notation is crazy? $Profile = new Profile(); // object variable starts with uppercase I've been running into some trouble where I assign an object to a variable, but then I want to use the same variable for a local value, and I'd like a standardized way to keep object variables separate. But I don't want to do something that another developer would find unintuitive or annoying if they had to work with my code. I've also considered something like this: $objects["profile"] = new Profile(); // all object variables are collected in an array But that's a lot slower to type. Thanks, -Arlo _______________________________ Arlo Leach 773.769.6106 http://arlomedia.com |
From: Andy C. <bng...@gm...> - 2009-09-02 12:10:03
|
At the risk of being the programming nazi, I'd recommend renaming either the variable or the class (such as ProfileObj). It'd be really confusing code with those similar names. Not sure what degree of code you are working with, but that's my 2 cents and I suspect it would eliminate any duplicate identifier errors. What exactly is the error you're getting? Thanks, Andy Carlson Carlson Technology (815) 200-9303 41° 22' 35" N 89° 28' 10" W --------------------------------------------------- "Man's conquest of Nature turns out, in the moment of its consummation, to be Nature's conquest of Man." -- C.S. Lewis --------------------------------------------------- "The ubiquity of the Internet is more important than the technology of the Internet" -- Jeff Bezos --------------------------------------------------- try { succeed(); } catch(E) { tryAgain(); } finally { readManual(); } On Wed, Sep 2, 2009 at 2:48 AM, Arlo Leach <ar...@ar...> wrote: > Hi folks, > > I guess my last question stumped you! Or confused you. Or you're all on > vacation this week. > > Anyway, here's another one. Does anyone think this kind of notation is > crazy? > > $Profile = new Profile(); // object variable starts with uppercase > > I've been running into some trouble where I assign an object to a variable, > but then I want to use the same variable for a local value, and I'd like a > standardized way to keep object variables separate. But I don't want to do > something that another developer would find unintuitive or annoying if they > had to work with my code. > > I've also considered something like this: > > $objects["profile"] = new Profile(); // all object variables are collected > in an array > > But that's a lot slower to type. > > Thanks, > -Arlo > > _______________________________ > > Arlo Leach > 773.769.6106 > http://arlomedia.com > > > > > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day > trial. Simplify your report design, integration and deployment - and focus > on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > _______________________________________________ > chiPHPug-discuss mailing list > chi...@li... > https://lists.sourceforge.net/lists/listinfo/chiphpug-discuss > |
From: Neil R. <Nei...@rc...> - 2009-09-02 14:31:19
|
Yes, it is kind of crazy: you're deliberately obfuscating your own code. Using the same name two different ways is shooting yourself in the foot. I'm a lousy typist myself, but it would never occur to me to try something like this. Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian Kernighan At 02:48 AM 9/2/2009, Arlo Leach <ar...@ar...> wrote: >Anyway, here's another one. Does anyone think this kind of notation is >crazy? > >$Profile = new Profile(); // object variable starts with uppercase Neil -- Nei...@rc... "Wait a minute, Juanita. This Snow Crash thing -- is it a virus, a drug, or a religion?" Juanita shrugs. "What's the difference?" -- _Snow Crash_, Neal Stephenson |
From: Arlo L. <ar...@ar...> - 2009-09-02 17:00:29
|
> It'd be really confusing code with those similar names. > Yes, it is kind of crazy: you're deliberately obfuscating your own code. > Using the same name two different ways is shooting yourself in the > foot. I'm a lousy typist myself, but it would never occur to me to > try something like this. All right, thanks for the sanity check. I could to something like $objProfile or $obj_profile, but I've always stayed away from that Hungarian notation kind of approach. The most common situation I've run into is that one variable name describes both an object and a member in a related class. For example, I have subscribers and some subscribers have profiles. Here's a super-simplified example: $subscriber = new Subscriber(); $profile = $subscriber->getValue("profile"); $profile = new Profile($profile); // oops, now $profile is ambiguous Since the profile member in the Subscriber class is a pointer to a separate Profile record, I guess most people would call that $profile_id. That's almost like Hungarian notation to me and I've never had the need to do that before. But now that I'm using objects a lot, my namespace has to accommodate one more type of data than before, so something has to give. By the way, I could just access $subscriber->profile rather than creating a local variable for $profile, but I like making the class members protected so I'm not tempted to set them directly and bypass any internal functionality in the class. If I could make the members read-only, that would work -- but PHP doesn't offer that, right? Cheers, -Arlo _______________________________ Arlo Leach 773.769.6106 http://arlomedia.com |
From: Andy C. <bng...@gm...> - 2009-09-02 18:16:51
|
As much as I might not approve of the last example you gave, it should work. In your example in your first message: $Profile = new Profile(); both names are in upper case. In the second example you gave: $profile = new Profile() they have different case so it should work. Just an observation. Do what you gotta do. PEACE!!!!!, Andy Carlson Carlson Technology (815) 200-9303 41° 22' 35" N 89° 28' 10" W --------------------------------------------------- "Man's conquest of Nature turns out, in the moment of its consummation, to be Nature's conquest of Man." -- C.S. Lewis --------------------------------------------------- "The ubiquity of the Internet is more important than the technology of the Internet" -- Jeff Bezos --------------------------------------------------- try { succeed(); } catch(E) { tryAgain(); } finally { readManual(); } On Wed, Sep 2, 2009 at 11:33 AM, Arlo Leach <ar...@ar...> wrote: > > It'd be really confusing code with those similar names. > > > Yes, it is kind of crazy: you're deliberately obfuscating your own code. > > Using the same name two different ways is shooting yourself in the > > foot. I'm a lousy typist myself, but it would never occur to me to > > try something like this. > > All right, thanks for the sanity check. I could to something like > $objProfile or $obj_profile, but I've always stayed away from that > Hungarian > notation kind of approach. > > The most common situation I've run into is that one variable name describes > both an object and a member in a related class. For example, I have > subscribers and some subscribers have profiles. Here's a super-simplified > example: > > $subscriber = new Subscriber(); > $profile = $subscriber->getValue("profile"); > $profile = new Profile($profile); // oops, now $profile is ambiguous > > Since the profile member in the Subscriber class is a pointer to a separate > Profile record, I guess most people would call that $profile_id. That's > almost like Hungarian notation to me and I've never had the need to do that > before. But now that I'm using objects a lot, my namespace has to > accommodate one more type of data than before, so something has to give. > > By the way, I could just access $subscriber->profile rather than creating a > local variable for $profile, but I like making the class members protected > so I'm not tempted to set them directly and bypass any internal > functionality in the class. If I could make the members read-only, that > would work -- but PHP doesn't offer that, right? > > Cheers, > -Arlo > > _______________________________ > > Arlo Leach > 773.769.6106 > http://arlomedia.com > > > > > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day > trial. Simplify your report design, integration and deployment - and focus > on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > _______________________________________________ > chiPHPug-discuss mailing list > chi...@li... > https://lists.sourceforge.net/lists/listinfo/chiphpug-discuss > |
From: Arlo L. <ar...@ar...> - 2009-09-02 19:03:31
|
Hi Andy, > both names are in upper case. In the second example you gave: > > $profile = new Profile() > > they have different case so it should work. Oh, the conflict isn't between the variable and the class name, but between the variable that I assign the object instance to and a regular variable I might have already created with the same name. All my class names start with uppercase; that's in 100% of the style guides I've seen, so I went with that. It's just the local variables I'm thinking about now. Basically now that I've introduced objects into my programming, I'm dealing with a new set of variables in each script, and I need to expand my naming standards to accommodate them. Cheers, -Arlo _______________________________ Arlo Leach 773.769.6106 http://arlomedia.com |
From: David R. <da...@ro...> - 2009-09-02 19:15:07
|
> > $subscriber = new Subscriber(); > $profile = $subscriber->getValue("profile"); > $profile = new Profile($profile); // oops, now $profile is ambiguous > > By the way, I could just access $subscriber->profile rather than creating a > local variable for $profile, but I like making the class members protected > so I'm not tempted to set them directly and bypass any internal > functionality in the class. If I could make the members read-only, that > would work -- but PHP doesn't offer that, right? > For situations like this, I'll use the __get($name) magic function. http://us2.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members This allows you to set a member variable as Private, and yet still allow $object->readonly syntax and functionality. Additionally, you easily avoid having to worry about ambiguous variable names when you only really wanted to get a read-only copy of a member variable. Hope that helps. -Rovani |
From: Richard L. <ce...@l-...> - 2009-09-09 18:11:37
|
You could write a __get() and no __set but some other "secret" way to change a value, and achieve something like a read-only member var. As far as $profile goes, I'd suggest: $subscriber = new Subscriber(); $subscriber_profile = $subscriber->getValue('profile'); $profile = new Profile($subscriber_profile); is probably the way to go. You most likely won't be using $subscriber_profile for long... Another option is to create a method on Subscriber that returns a Profile object, and don't ever mess directly with the value at all. On Wed, September 2, 2009 11:33 am, Arlo Leach wrote: >> It'd be really confusing code with those similar names. > >> Yes, it is kind of crazy: you're deliberately obfuscating your own >> code. >> Using the same name two different ways is shooting yourself in the >> foot. I'm a lousy typist myself, but it would never occur to me to >> try something like this. > > All right, thanks for the sanity check. I could to something like > $objProfile or $obj_profile, but I've always stayed away from that > Hungarian > notation kind of approach. > > The most common situation I've run into is that one variable name > describes > both an object and a member in a related class. For example, I have > subscribers and some subscribers have profiles. Here's a > super-simplified > example: > > $subscriber = new Subscriber(); > $profile = $subscriber->getValue("profile"); > $profile = new Profile($profile); // oops, now $profile is ambiguous > > Since the profile member in the Subscriber class is a pointer to a > separate > Profile record, I guess most people would call that $profile_id. > That's > almost like Hungarian notation to me and I've never had the need to do > that > before. But now that I'm using objects a lot, my namespace has to > accommodate one more type of data than before, so something has to > give. > > By the way, I could just access $subscriber->profile rather than > creating a > local variable for $profile, but I like making the class members > protected > so I'm not tempted to set them directly and bypass any internal > functionality in the class. If I could make the members read-only, > that > would work -- but PHP doesn't offer that, right? > > Cheers, > -Arlo > > _______________________________ > > Arlo Leach > 773.769.6106 > http://arlomedia.com > > > > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > 30-Day > trial. Simplify your report design, integration and deployment - and > focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > _______________________________________________ > chiPHPug-discuss mailing list > chi...@li... > https://lists.sourceforge.net/lists/listinfo/chiphpug-discuss > -- Some people ask for gifts here. I just want you to buy an Indie CD for yourself: http://cdbaby.com/search/from/lynch |