0

I have two functions within a PHP class. I want to use the output of the first function within the second, but I always get an internal server error.

Simplified example:

<?php 
class TestClass{

    private function return_string(){
        $test = 'test variable';

        return $test;
    }

    private function encode_string() {
        $x = return_string();

        return json_encode($x);
    }
}

?>

I've tried returning the string directly from return_string() (not that this is what I want) and even this still gives an error. If I set $x to a string instead of the function return_string() it works fine, so everything in the encode_string() function seems fine. Not sure what else to check.

2 Answers 2

2

Use this

$x = $this->return_string();
Sign up to request clarification or add additional context in comments.

Comments

1

Since both methods are of same class you can call it by $this keyword.
$this->return_string(); will do the job.

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.