Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,108
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Nested Functions

    Am I breaking any kind of design principle if I call one Function from within another Function?


    Debbie

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Nope. But if you nest your functions the nested function signature will not be available until after the enclosing function has been called.
    PHP Code:
    <?php

    function outer()
    {
       function 
    inner()
       {
       }
    }

    ReflectionFunction::export('outer');
    try
    {
        
    ReflectionFunction::export('inner');
    }
    catch (
    Exception $ex)
    {
        print 
    $ex->getMessage() . PHP_EOL;
    }

    outer();

    ReflectionFunction::export('outer');
    ReflectionFunction::export('inner');
    Code:
    Function [ <user> function outer ] {
      @@ /t.php 3 - 8
    }
    
    Function inner() does not exist
    Function [ <user> function outer ] {
      @@ /t.php 3 - 8
    }
    
    Function [ <user> function inner ] {
      @@ /t.php 5 - 7
    }

  • #3
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    doulbedee are you asking about this sort of nesting?


    PHP Code:
    <?php

    function one() {
    }

    function 
    two() {
    one();
    }


    two();

    ?>
    Leonard Whistler

  • #4
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,108
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Len Whistler View Post
    doulbedee are you asking about this sort of nesting?


    PHP Code:
    <?php

    function one() {
    }

    function 
    two() {
    one();
    }


    two();

    ?>
    Exactly!


    Debbie

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Not doing so would effectively invalidate the purpose of writing functions. The more you do this the more centralized the code becomes which makes a single point of entry for a single point of instruction. This way if something changes or is altered it only needs to be done in one place, not in many places.

    You can chain your functions together as much as you want, just make sure you don't get it stuck in an uncontrolled recursive stack or a cyclical call and all will be well.

  • #6
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,108
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Not doing so would effectively invalidate the purpose of writing functions. The more you do this the more centralized the code becomes which makes a single point of entry for a single point of instruction. This way if something changes or is altered it only needs to be done in one place, not in many places.

    You can chain your functions together as much as you want, just make sure you don't get it stuck in an uncontrolled recursive stack or a cyclical call and all will be well.
    Okay, just being sure.

    To quote a current colleague, "We strive to be effective first, and efficient second."

    I think my PHP coding has reached "effectiveness", and now I want to try to start being more "efficient".

    And if I can get v2.0 wrapped up, and then start learning and applying OOP to v3.0, I hope I can make great leaps forward in my coding abilities, but we'll see?!

    Thanks,


    Debbie

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Sounds logical to me. That's the whole learn to walk before you run paradigm; in time you think of how to run first.
    Doing this will lead much easier into OOP. Its not the same as its an entirely different paradigm, but classes are literally little datatype collections which chain methods together with properties. If you can do it in OOP, you can do it with (usually) far less code and work in procedural.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •