0

I'm trying to call a function getBMR() which is in another file (user.php) with the following code and I'm getting an error:

Using $this when not in object context )..

variables are set in the process.php file that I am calling the function in

$a =$_POST["example"];

Here is the code

echo $this->getBMR($a,$b,$c,$d,$e,$f);

Thanks, any help would be great.

class user {

public function getBMR()
{
    switch ($this->gender){
                case 'Female':
                    $gender= 655 + (9.6 * $userWeight ) + (1.8 * $userHeight) - (4.7 * $userAge);
                    echo "<p>Your estimated daily metabolic rate is $gender </p>";
                    echo "<p>This means that you need rouhgly $gender calories a day to maintain your current weight.</p>";

                    break;
                    case 'Male':
                        $gender=66 + (13.7 *$userWeight) + (5 * $heuserHeightight) - (6.8 * $userAge);
                        echo "<p>Your estimated daily metabolic rate is $gender </p>";
                        echo "<p>This means that you need rouhgly $gender calories a day to maintain your current weight.</p>";

            }

}

2
  • Try require(user.php); at the top of process.php Commented Dec 7, 2019 at 20:50
  • can you show us the class which defines method getBMR(), the way you instantiate it (e.g. $obj = new someClass();) and how you're trying to use method getMBR() Commented Dec 7, 2019 at 21:21

1 Answer 1

1

I suspect this code is not part of class method. Try this

require_once("user.php");
$a =$_POST["example"];
// rest of code
echo getBMR($a,$b,$c,$d,$e,$f);

you are not allowed to use "$this" since you are not in an object context which means you are not using "$this" within class method

Note that if "user.php" file is not in same directory as process.php you need to include relative/absolute path up to directory in which user.php file is such as:

require_once('../path/to/users/user.php');

Taken from https://www.php.net/manual/en/language.oop5.basic.php

The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object

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

6 Comments

Tried require_once it didnt work.. i have include at the moment and still nothing
Are you still getting any errors in your code? If so, write them down please. Also did you remove $this-> from the method call? Check logs for any additional errors please.
Call to undefined function getBMR() is theerror im getting
I have removed $this-> from mehod file too
Can you include user.php file into the original question? We need to see the method definition
|

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.