0

Dear all,
I am confused about the output of the below programs .When i use the scope resolution operator i am getting the below error.

<?
class MyClass{
        public $variable_value="This is a variable value ";
         function display(){
                echo "Display Function \n";
                echo $this->variable_value;
        }
}
MyClass::display();
//$obj=new MyClass();
//$obj->display();
?>

output:
Display Function
PHP Fatal error: Using $this when not in object context in /var/www/html/programs/class_scope.php on line 6

When i use the object i am not getting the error .The program works fine .What is the major difference between those two programs.

<?
class MyClass{
        public $variable_value="This is a variable value ";
         function display(){
                echo "Display Function \n";
                echo $this->variable_value;
        }
}
//MyClass::display();
$obj=new MyClass();
$obj->display();
?>

Difference between scope resoultion operator and object?Can any one explain about this criteria?

Thank you,

With Regards,
Prem

3
Contributors
8
Replies
9
Views
6 Years
Discussion Span
Last Post by prem2
0

Your display function refers to $this (the current object). If you try to use the scope operator (trying to access a class function), $this is not defined, as there is no object instantiated.

0

Dear pirataes,
I have understood your explanation.But when i implement the same program with little modification given by your explanation .I am still receiving the fatal error.

That is when i first create the object of the class .Then i use the scope resolution operator to call the function .Still receiving the bug.

<?
class MyClass{
        public $variable_value="This is a variable value ";
         function display(){
                echo "Display Function \n";
                echo $this->variable_value;
        }
}
$obj=new MyClass();
MyClass::display();
?>

output:
Display Function
PHP Fatal error: Using $this when not in object context in /var/www/html/programs/class_scope.php on line 6

Thank you,

With Regards,
Prem

0

The same still applies, you cannot use $this within a class function. There is no link between lines 9 and 10.

The first line in the manual says it: "... allows access to static, constant, and overridden properties or methods of a class"

<?php
class MyClass{
  public static $variable_value = "This is a variable value";

  public static function display(){
    echo "Display Function \n";
    echo self::$variable_value;
  }
}
MyClass::display();
?>

Edited by pritaeas: n/a

0

Dear pirataes,
I have understood your explanation.But when i implement the same program with little modification given by your explanation .I am still receiving the fatal error.

That is when i first create the object of the class .Then i use the scope resolution operator to call the function .Still receiving the bug.

output:
Display Function
PHP Fatal error: Using $this when not in object context in /var/www/html/programs/class_scope.php on line 6

Thank you,

With Regards,
Prem

<?
class MyClass{
        public $variable_value="This is a variable value ";
         function display(){
                echo "Display Function \n";
                echo $this->variable_value;
        }
}
$obj=new MyClass();
$obj->display();
?>
0

Dear pirataes,
I have understood your explanation.But when i implement the same program with little modification given by your explanation .I am still receiving the fatal error.

That is when i first create the object of the class .Then i use the scope resolution operator to call the function .Still receiving the bug.


output:
Display Function
PHP Fatal error: Using $this when not in object context in /var/www/html/programs/class_scope.php on line 6

Thank you,

With Regards,
Prem

Or use another variant using the magic __toString method

<?
class MyClass{
        public $variable_value="This is a variable value ";
         function __toString(){
                $str = "Display Function \n";
                $str.= $this->variable_value;
                return $str;
        }
}
$obj=new MyClass();
echo $obj;
?>

Edited by maba001: code fix

1

Dear pirataes,
I have understood your explanation.But when i implement the same program with little modification given by your explanation .I am still receiving the fatal error.

That is when i first create the object of the class .Then i use the scope resolution operator to call the function .Still receiving the bug.

<?
class MyClass{
        public $variable_value="This is a variable value ";
         function display(){
                echo "Display Function \n";
                echo $this->variable_value;
        }
}
$obj=new MyClass();
MyClass::display();
?>

output:
Display Function
PHP Fatal error: Using $this when not in object context in /var/www/html/programs/class_scope.php on line 6

Thank you,

With Regards,
Prem

Actually I should have started reading with the original post.

With the scope resolution operator you sort of leave the object context. You address the class directly. In your case you basically use the class as a "function repository". A class method that is called via the scope resolution operator does not have access to any kind of object. It can only access things defined at the class level.

With the object context you basically use the class as a blueprint and you either explicitly or implicitly invoke the constructur using the "new" keyword.

So $obj1 = new MyClass; and $obj2 = new MyClass; will create two copies from the MyClass blueprint. Once they are instanciated they each live a life of their own. Which means your class attribute variable_value can be different for both objects (two different memory locations in your servers RAM).
Inside the blueprint (the class definition) $this is the pointer to the specific object (either $obj1 or $obj2 in this example).

In contrast if you do never invoke "new" on MyClass, you never create an object. So all manipulations that you do are done in the "blueprint", the class itself.
Therefore in this case you cannot use $this at all, which is what the error message says, as there is no object and no pointer to an object.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.