[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Fatal error: Using $this when not in object context in

Posted on 2011-04-18
Medium Priority
Last Modified: 2012-06-21
Fatal error: Using $this when not in object context in


i want to use $this
because I am copy pasting it from other code
Question by:rgb192
LVL 19

Accepted Solution

Greg Alexander earned 668 total points
ID: 35419685
$this-> this is only available if you are in a class... you can start the class above that and set it to your own variable such as:

include "some_class.php";
$new_var = new some_class();

$new_var->name = name;

Open in new window

LVL 48

Assisted Solution

hernst42 earned 668 total points
ID: 35420075
$this is only available in methods and not in functions. Eg. what works:

class A {
protected $name = 'foo';
public function inc() {
  include 'name.php';
  return $this;
public function out() {
  echo $this->name;

$x = new A();

$this->name = 'bar';
LVL 111

Expert Comment

by:Ray Paseur
ID: 35420790
You might need to copy/paste a larger section of code, perhaps including all the class definition.
LVL 12

Assisted Solution

by:Mohamed Abowarda
Mohamed Abowarda earned 664 total points
ID: 35420903
If your code $this->name = 'name'; is NOT inside class, you must use the class name instead.

If you are using static variables you can use:
$className::name = 'name';

Open in new window

If you are using instance of the class, you can use:
$className->name = 'name';

Open in new window

To declare a class instance (change "YourClassName" with the name of your class):
$className = new YourClassName;

Open in new window


Author Closing Comment

ID: 35448715

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question