1

I have a Response.php file which has a function inside and i want to call the function to another .php file.

here's the Response.php

<?php
include 'connection.php';
$response = array();
class Response_Function{
 public function fieldMissing() {
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
    $response = json_encode($response);

    echo $response;
    }
}

?>

and this is the file calling the fieldMissing() function

<?php
include 'connection';
require_once 'include/Response.php';
$db = new Response_Function();
$response = $db->fieldMissing();
echo $response;
?>
6
  • 2
    What's wrong - happening now vs what you want to happen? Though I think inside the definition of fieldMissing() you might want return $response; instead of echo $response;, because as it is the function doesn't return anything. Therefore, in the second file $response will have no useful value assigned to it. Commented Jan 30, 2014 at 0:46
  • what do you nean and?? Commented Jan 30, 2014 at 0:47
  • i want to call the fieldMissing function. how will i call it Commented Jan 30, 2014 at 0:47
  • 4
    You are already calling it! Commented Jan 30, 2014 at 0:48
  • but there's no response appear .. Commented Jan 30, 2014 at 0:50

3 Answers 3

2

Like BrianDHall said, in your function you need to replace echo with return

Sign up to request clarification or add additional context in comments.

Comments

2

You shouldn't echo in a function always use return instead and let the caller echo wherever it needs.

Response.php

    <?php

    include 'connection.php';

    class Response_Function
    {

        public $response;

        public function fieldMissing() {

            $response["success"] = 0;
            $response["message"] = "Required field(s) is missing";

            $this->response = json_encode($response);

    }
}

file calling the function fieldMissing()

   <?php

   include 'connection';
   require_once 'include/Response.php';

   $db = new Response_Function();
   $db->fieldMissing(); 

   $response = json_decode($db->response);

   echo $response->message;

  ?>

Comments

0

There are two problems with your code:

1) The response array should be declared INSIDE the function (however if you need to access it from elsewhere in the class, declare it and use it as a class attribute INSIDE the class);

2) As others have pointed out, the last instruction of your function should be returning the value instead of printing it.

To summarize, your Response.php file should look like this:

<?php
include 'connection.php';
class Response_Function{
  public function fieldMissing() {
    $response = array();
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
    $response = json_encode($response);

    return $response;
  }
}

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.