0

i am trying to call a function which is nested under other function consider a example:

function _get_stats() {
    function _get_string() {
        $string = 'Nested Function Not Working';
        return $string;
    }
 }

 public function index() {
    $data['title'] = $this->_get_stats() . _get_string();
    $this->load->view('home', $data);
 }

now when i run the page in web browser blank page is displayed.

any suggestion or help would be a great help for me.. thanks in advance

4
  • try this line $data['title'] = $this->_get_stats() . $this->_get_string(); I have never find any point in using nested functions... (not closures). Commented Apr 11, 2014 at 15:58
  • Can you explain why you're doing it this way? I can't see a benefit to this... Commented Apr 11, 2014 at 16:03
  • @Kryten i just wanted to know how nested functions works in php. Commented Apr 11, 2014 at 16:04
  • 1
    I see. Thanks! Now I understand better how they work too :-) Commented Apr 11, 2014 at 17:09

2 Answers 2

1

The function is not really nested, but calling _get_stats() will cause _get_string to be declared. There is no such thing as nested functions or classes in PHP.

Calling _get_stats() twice or more will cause an error, saying that function _get_string() already exists and cannot be redeclared.

Calling _get_string() before _get_stats() will raise an error saying that function _get_string() does not exist`.

In your case, if you really want to do this (and it is a bad practice), do the following:

protected function _get_stats() {
    if (!function_exists("_get_string")){
        function _get_string() {
            $string = 'Nested Function Not Working';
            return $string;
        }
    }
}

public function index() {
    $this->_get_stats(); //This function just declares another global function.
    $data['title'] = _get_string(); //Call the previously declared global function.
    $this->load->view('home', $data);
}

BUT What you are looking for is probably method chaining. In this case you method must return a valid object which contains the needed functions.

Example:

protected function getOne(){
  //Do stuff
  return $this ;
}

protected function getTwo(){
  //Do stuff ;
  return $this ;
}

public function index(){
  $this
    ->getOne()
    ->getTwo()
  ;
}
Sign up to request clarification or add additional context in comments.

2 Comments

please could you explain me why it is a bad practice?
Because messing with function declarations is just bad, code will be unmaintanable.
1

If you have a blank page, it may be a 500 "server error" response, i.e. fatal error in the PHP code.

_get_string will be defined when PHP execution reaches its declaration, i.e. in _get_stats when this one is executed.

In index() , _get_string probably is not declared yet at the moment you're invoking it.

As nested functions are nevertheless defined in the global namespace (contrary to JS for example), you may want to move the _get_string declaration.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.