SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: function

  1. #1
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    323
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    function

    i am following a tutorial for a log in script, but what i don't understand is ow the array from the $config was passed to the $path. please pardon me if its like a novice questions , i have read about functions over again , but still did not get it.can anyone help
    PHP Code:
    <?php
    class Config {
        public static function 
    get($path null) {
            if(
    $path) {
                
    $config $GLOBALS['config'];
                
    $path explode('/'$path);

                foreach(
    $path as $bit) {
                    if(isset(
    $config[$bit])) {
                        
    $config $config[$bit];
                    }
                }

                return 
    $config;
            }

            return 
    false;
        }
    }

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    66 Post(s)
    Tagged
    2 Thread(s)
    http://www.php.net/manual/en/reserve...es.globals.php

    $GLOBALS is accessible anywhere, there is no scope. So inside the function, $config is set to $GLOBALS['config']. Whether config was defined or not, I'm not sure.

    Are you passing an argument when calling the get function?

  3. #3
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    351
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    umm your code has two flaws you probably want to know of at this stage, since if you develop a habit of coding in this way you will be in serious trouble in future. First of all, global variables a bad bad practices, and superglobal arrays should be handled much more carefully. I do not recommend messing with the $GLOBALS array, it brings more trouble than its worth. Also, static methods are poor OOP practices, you should try to avoid that too.

  4. #4
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    323
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Config was defined in another file

    Code:
    $GLOBALS['config'] =array( 	'mysql' => array( 		'host' 		=> ' ', 		'username' 	=> ' ', 		'password' 	=> ' ', 		'db' 		=> ' ' 	),
    The path i would have like to understand is
    Code:
    $path*=*explode('/',*$path);**
    I was wondering when the array in the global was assigned to $path ?

    I am replying from a tablet, so some typing might not come out well

  5. #5
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    323
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    @ hall of farmer ,i just started learning oop, pardon my practice i was following a tutorial will check that out too.thanks


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
  •