Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: php function trouble

  1. #11
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Quote Originally Posted by jscheuer1 View Post
    It is equivalent for the example as first presented by Nile. In your example $xml isn't accessible anywhere I can see. You would have to make a getter function and/or $xml would have to be assigned as a property of something in order to be both accessible and not global.
    That's true for the specific example I provided, however we can't assume the context in which the OP will be using the code we provide. As a general rule, it's better not to clutter any global scopes. My initial reaction to this thread and your response was that using a reference would be better than using a global variable unless there are sophistic intentions that the OP didn't mention. My post wasn't a direct response to what the OP wanted, but instead just a (in my opinion) better alternative to using a global variable within load_sw_popup()'s scope.

    Generally, though, I do agree with djr33 that returning a value is in most times better than cluttering different scopes or using a reference (which can often result in confusion).

  2. #12
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I will for now defer to both you and Daniel's take in PHP. In javascript I prefer to create scopes and/or assign values, objects, functions, etc. to a single object or function that gives accessibility from the global scope without polluting it with more than one variable or other named entity. Sometimes I prefer using no access to anything other than via an instance that's created for one or more page element(s), accessing it via that element(s) when needed. But that approach can be too limiting even in javascript and doesn't appear to have any direct equivalent in PHP, which never assigns anything to a page element (window, document or a tag). At least not that I know of.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #13
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by jscheuer1 View Post
    I will for now defer to both you and Daniel's take in PHP. In javascript I prefer to create scopes and/or assign values, objects, functions, etc. to a single object or function that gives accessibility from the global scope without polluting it with more than one variable or other named entity. Sometimes I prefer using no access to anything other than via an instance that's created for one or more page element(s), accessing it via that element(s) when needed. But that approach can be too limiting even in javascript and doesn't appear to have any direct equivalent in PHP, which never assigns anything to a page element (window, document or a tag). At least not that I know of.
    You can scope variables like that in PHP, but you have to get into object-oriented programming to do it. However, using objects to "scope" variables and functions isn't as convenient (or efficient) in php as it is in js. It has a lot to do with php using classical inheritance (class definitions) rather than prototypical inheritance (classes based on a literal, existing "prototype" object). Also with the fact that objects are basically an afterthought in php.

    Not counting objects, you basically have two scopes in PHP: global and function, and they're largely exclusive. From a practical standpoint, it's not possible to avoid "littering" the global scope.

  4. #14
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You can scope variables like that in PHP, but you have to get into object-oriented programming to do it. However, using objects to "scope" variables and functions isn't as convenient (or efficient) in php as it is in js. It has a lot to do with php using classical inheritance (class definitions) rather than prototypical inheritance (classes based on a literal, existing "prototype" object). Also with the fact that objects are basically an afterthought in php.
    Hm, that makes sense, but I would have never thought of it that way. So to respond to John, there is a clear difference both in the design of PHP and JS, and how programmers of each think about designing these sorts of operations. Interesting.

    Not counting objects, you basically have two scopes in PHP: global and function, and they're largely exclusive. From a practical standpoint, it's not possible to avoid "littering" the global scope.
    True. In order to avoid that, you'd need to create a main function to host the whole process. There's nothing technically wrong with that, but it would be kind of weird. (A relatively un-invasive way to do with would be by having a function that includes another page, then putting all of your code in there. Then the initial page would just pick a page, and also put it in a function-limited scope. Odd, but possible.)

    The result for PHP is that in order to avoid naming conflicts, we can do three things:
    1. Use prefixes on variables, like $dd_foo and $dd_bar if we're working on something related to Dynamic Drive. This isn't foolproof, of course, because it just works on the basis of hoping that all prefixes will be the same. (The logic is equivalent to database table naming.)
    2. [My favorite] Use arrays to store specific-topic values, such as $dd['foo'] and $dd['bar']. Assuming you know that $dd is available, now you can store everything you need in that. For example, when working with login systems, I often have something like $user available in the global scope, but lots of 'variables' inside it (as an indexed array).
    3. Use constants instead of variables. These are just like variables (but without the $) except that they can never be changed. The benefit of this is that you will get an error if you try to change them, so you can never have a naming conflict-- or, more accurately, you'll know if you have a naming conflict. These are often used in third-party software (eg, forums) where something like __DBNAME__ might be defined, etc. (The __ prefixes are used to further differentiate them.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Similar Threads

  1. Adding javascript function within ontoggle function
    By piers in forum Dynamic Drive scripts help
    Replies: 6
    Last Post: 07-22-2009, 02:41 AM
  2. Trouble passing variables to another function
    By polyglot in forum JavaScript
    Replies: 10
    Last Post: 06-05-2009, 02:17 PM
  3. Replies: 9
    Last Post: 09-16-2008, 05:31 PM
  4. Trouble with function not receving params in FF
    By changemystrings in forum JavaScript
    Replies: 4
    Last Post: 07-27-2008, 12:53 AM
  5. Passing Variables Function to Function
    By jscheuer1 in forum JavaScript
    Replies: 3
    Last Post: 03-26-2005, 12:22 AM

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
  •