Is that possible to get $this using another variable?

257 Views Asked by At

So, I am newbie in php so I feel litle confused right now.

I have a joomla! site with K2 extension. I have $this->item->imageXLarge; inside K2 item.php. I need to get $this->item->imageXLarge; outside of my item.php, but exactly in same page (in a module that is rendering in current image).

What I really tried out:

  • $k2itemimage = $this->item->imageXLarge; - at the top of my item.php
  • echo $k2itemimage - inside my module, outside my item.php

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

Any idea of how could I get $this variable of current imageXLarge?

EDIT -> setDefaultImage class

public static function setDefaultImage(&$item, $view, $params = NULL)
    {
        if ($view == 'item')
        {
            $image = 'image'.$item->params->get('itemImgSize');
            $item->image = $item->$image;

            switch ($item->params->get('itemImgSize'))
            {

                case 'XSmall' :
                    $item->imageWidth = $item->params->get('itemImageXS');
                    break;

                case 'Small' :
                    $item->imageWidth = $item->params->get('itemImageS');
                    break;

                case 'Medium' :
                    $item->imageWidth = $item->params->get('itemImageM');
                    break;

                case 'Large' :
                    $item->imageWidth = $item->params->get('itemImageL');
                    break;

                case 'XLarge' :
                    $item->imageWidth = $item->params->get('itemImageXL');
                    return $k2itemimage = $item->params->get('itemImageXL');
                    break;
            }
        }
1

There are 1 best solutions below

6
On

At the top of your page write: $k2itemimage = null;

then when you want to set this variable inside your object simply write:

global $k2itemimage;
$k2itemimage = $this->item->imageXLarge;

Here is a function to check whether a variable has anything inside of it:

function die_var($data, $informativeButNotPretty=False) {
    if($informativeButNotPretty) {
        echo '<pre>';
        var_dump($data);
        die('</pre>');
    }
    else {
        die('<pre>'.print_r($data,true).'</pre>');
    }
}

Put that right at the top of your script, and where ever you are trying to assign the global var simply put on the line above:

die_var($this->item->imageXLarge,true);