Fatal error: Using $this when not in object context

Hi Guys,

is the first time i’m using yii framework, so i got the code on the site and extract them to a paste and run the code.

and i got a problem:

Fatal error: Using $this when not in object context in C:\xampp\htdocs\yii\basic\views\site\index.php on line 3

that is in this line:

$this->title = ‘My Yii Application’;

and this occurs in the basic and advanced version, someone knows what is happening?

Thanks,

Pedro.

You really need some basics about objects in PHP, here is the documentation that explains what $this is - http://php.net/manual/en/language.oop5.basic.php

"got the code on the site and extract them to a paste and run the code" - is just go to the browser and put the url to main folder, where is the index.php.

i did nothing i just extract the code and "run" them.

You really need some basics about PHP and objects, please read the documentation on the link I gave you above… you cannot use $this if you are not in a class… you obviously took some code from a class and put it outside a class… that cannot work.

better is if you install yii using composer.

Installing via Composer

I know that, but like i said i didn’t change anything in the code, i just wanted to run them in the browser so i think is a yii problem no?

Have you tried to open this file directly in your browser or execute it as a standalone PHP scipt in your IDE/debugger? If so - it’s not the way it supposed to work. Entry scripts guide.

Hi Maurizio, I know that this topic is very old, but I’m here because I had the same error.

But check this.

I used Gii to generate my views. I didn’t made any change in the files, when I test, Yii return this error




==> /var/log/apache2/error.log <==

[Thu Sep 29 14:25:30.585912 2016] [:error] [pid 16722] [client 127.0.0.1:46732] PHP Fatal error:  Using $this when not in object context in /var/www/html/historia_clinica/backend/web/provincia/index.php on line 12


==> /var/log/apache2/other_vhosts_access.log <==

backend.dev:80 127.0.0.1 - - [29/Sep/2016:14:25:30 -0300] "GET /provincia/ HTTP/1.1" 500 185 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0"



view/index.php




<?php


use yii\helpers\Html;

use yii\grid\GridView;

use yii\widgets\Pjax;

use yii\web\View;


/* @var $this yii\web\View */

/* @var $searchModel backend\controllers\provinciaSearch */

/* @var $dataProvider yii\data\ActiveDataProvider */


$this->title = 'Provincias';

$this->params['breadcrumbs'][] = $this->title;

?>

<div class="provincia-index">


    <h1><?= $this->title ?></h1>

    <?php echo $this->render('_search', ['model' => $searchModel]); ?>


    <p>

        <?= Html::a('Create Provincia', ['create'], ['class' => 'btn btn-success']) ?>

    </p>

<?php Pjax::begin(); ?>    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'idprovincia',

            'nombre',


            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>

<?php Pjax::end(); ?></div>



controller.php




<?php


namespace backend\controllers;


use Yii;

use backend\models\Provincia;

use backend\controllers\provinciaSearch;

use yii\web\NotFoundHttpException;

use yii\filters\VerbFilter;

use yii\web\Controller;


/**

 * ProvinciaController implements the CRUD actions for Provincia model.

 */

class ProvinciaController extends Controller

{

    /**

     * @inheritdoc

     */

    public function behaviors()

    {

        return [

            'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'delete' => ['POST'],

                ],

            ],

        ];

    }


    /**

     * Lists all Provincia models.

     * @return mixed

     */

    public function actionIndex()

    {

        $searchModel = new provinciaSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


        return $this->render('index', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }



I check the others files views to check how to work, and the structure is the same. In the view has the same variable "$this"…

I don’t understand why isn’t works my view. I have PHP 7.0.8.

I’ll appreciate your help with this

Hi turco_7

Not sure what could be the reason… try with the classic url like


 http://localhost/yourapp/backend/web/index.php?r=provincia/index

*adjast the path to your app

You are right… I disabled the PrettyUrl and tested with the url like you says, and is working fine. But I’d like to work with PrettyUrl.

great… could be you are missing something for the pretty url… maybe the .htaccess ?

Yes … I don’t have the .htaccess file in my root folder… Thanks!