Andy Khatter
Andy Khatter

Reputation: 165

Unable to call a class in php

<?php
try{

    $test = new TestAccessModifiers("2345","xyz","vfd","a0001","99","67"); /*invoking the class*/

    var_dump($test->calculate());
}
catch(Exception $e){

    echo $e->getMessage();
}
?>

<?php 
class TestAccessModifiers {


    function TestAccessModifiers($user_p,$user_fn,$user_ln,$user_id,$marks1,$marks2) {
        echo "hello1";
        $this->user_phone=$user_p;
        $this->user_fname=$user_fn;
        $this->user_lname=$user_ln;
        $this->user_id=$user_id;
        $this->marks1=$marks1;
        $this->marks2=$marks2;


        echo $this->marks1;
    }
    private  $additional_marks = 10;
    public static function calculate(){


        return $this->marks1+$this->marks2+$this->getAdditionalmarks();

    }

    public function getAdditionalmarks(){

        return $this->additional_marks;
    }


}
?>

Above is the simple code i am trying to run... but i am unable to call TestAccessModifiers I have tried using _constructor too

Upvotes: 0

Views: 408

Answers (5)

Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25745

if you are calling the class in another php page, make sure you include it like this.

include('/path/to/your/class.php');
$test = new TestAccessModifiers("2345", "xyz", "vfd", "a0001", "99", "67");

or if you are instantiating the object within the same file then place the instantiation code below your class.

class TestAccessModifiers {
    public function __construct($user_p, $user_fn, $user_ln, $user_id, $marks1, $marks2) {
        echo "hello1";
        $this->user_phone = $user_p;
        $this->user_fname = $user_fn;
        $this->user_lname = $user_ln;
        $this->user_id = $user_id;
        $this->marks1 = $marks1;
        $this->marks2 = $marks2;
        echo $this->marks1;
    }
    private $additional_marks = 10;

    public function calculate() {
        return $this->marks1 + $this->marks2 + $this->getAdditionalmarks();
    }
    public function getAdditionalmarks() {
        return $this->additional_marks;
    }
}

try {
    $test = new TestAccessModifiers("2345", "xyz", "vfd", "a0001", "99", "67"); /*invoking the class*/
    var_dump($test->calculate());
} catch (Exception $e) {
    echo $e->getMessage();
}

you have defined a static method in your class, and have used the pseudo variable $this inside of the static method, which PHP does not allow. since static method is treated out of object context in PHP. you need to remove the static method to use $this

Upvotes: 1

Rhys
Rhys

Reputation: 1491

public static function calculate() {
    return $this - > marks1 + $this - > marks2 + $this - > getAdditionalmarks();
}

You can't operate on $this from a static context. Make this method non-static, or adjust the other variables to be static, whichever suits your context.

Upvotes: 0

Niels Thiebosch
Niels Thiebosch

Reputation: 147

What version of PHP are we talking about? First thing comes to mind: did you add it to autoload ? I would use the __constructor methode as initiator routine of the class.

Upvotes: 0

Estefano Salazar
Estefano Salazar

Reputation: 419

First: Which PHP version you have? Second: Are you including the class file in the file that you are instancing? Third: Your function "calculate" is static, then, you can't access to it from an instance and it have no way to read or write properties non-statics.

Try TestAccessModifiers::calculate(); and put in a simple return "Hello World"

Greetings.

Upvotes: 0

Strae
Strae

Reputation: 19445

Rename your TestAccessModifiers function to __construct.

public function __construct($user_p,$user_fn,$user_ln,$user_id,$marks1,$marks2) {
    echo "hello1";
    $this->user_phone = $user_p;
    $this->user_fname = $user_fn;
    $this->user_lname = $user_ln;
    $this->user_id = $user_id;
    $this->marks1 = $marks1;
    $this->marks2 = $marks2;
    echo $this->marks1;
}

Then, remove static from calculate function.

It should then works..

Reference: http://php.net/manual/en/language.oop5.decon.php

Upvotes: 2

Related Questions