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

Posted on 2011-04-18
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 18

    Accepted Solution

    $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

    $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 107

    Expert Comment

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

    Assisted Solution

    by:Mohamed Abowarda
    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now