3

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;
            }
        }
fiskolin
  • 1,421
  • 2
  • 17
  • 36
  • I have never used Joomla or K2 but have you tried setting up a Session? This is commonly used to pass information from one php page to another. With that being said, not sure if this solves your problem because it is telling you that it does not know where the object is. So if you are using `$this`, what is your container object you are trying to use `$this` in? – j0hnstew Jan 06 '14 at 16:58
  • @stewbydoo Hmm, I was searching for _HOW TO GET SESSION VARIABLES_ and didn't find anything easy for _newbies_. Can you please let me now the process? I mean, set the Session and after that echo/get the variable? – fiskolin Jan 06 '14 at 17:13
  • Can you add your item.php class in your question? – Mike Brant Jan 06 '14 at 17:23
  • @MikeBrant `item.php` is a file to render my frontend HTML. I edited my question with setDefaultImage class. – fiskolin Jan 06 '14 at 17:33
  • @user3086817 OK so if there is a static function to retrieve this image, why don't you just call it (i.e. `$k2itemimage = classname::setDefaultImage($item, 'item', array('itemImgSize' => 'XLarge'));` or something similar). I don't know the class names or usage of `$params` here to give full answer. – Mike Brant Jan 06 '14 at 17:38
  • $this has to be used INSIDE a class. If you want to access an instance outside of the class itself, you have to use the variable instance like `$image = new Image(); $image-> ...`. I discourage to use static functions, you have to use them with `Image::` and inside: `self::` – Daniel W. Jan 06 '14 at 17:42
  • @MikeBrant, that gives me a fatal error, in both pages (so I guess that I tried out in wrong way). `Call to a member function get() on a non-objec`. I just change `classname` to `K2HelperUtilities`. – fiskolin Jan 06 '14 at 17:48
  • @DanFromGermany What do you mean, _you have to use them with `Image::` and inside `self::`?_ – fiskolin Jan 06 '14 at 17:50
  • @user3086817 if you access a static function without an object instance, you use `ClassName::yourStaticFunction();`, inside a static called function, you would use `self::yourStaticFunction();`. In scope outside of an INSTANCE of a class, you use `$classInstance->classMethod();` and only inside an instance (an instance is a class instanced by `new ClassName();`) you would use `$this->memberMethod();` – Daniel W. Jan 06 '14 at 18:01
  • @stewbydoo Your idea saved me! It's really possible to define `$this` as another variable and set a _SESSION_ to get in another file. :) Can you please post that idea in an answer so I can accept? :) – fiskolin Jan 06 '14 at 19:35

1 Answers1

-1

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);
RaggaMuffin-420
  • 1,762
  • 1
  • 10
  • 14
  • Now the **FATAL ERROR:** disappears, but echo's blank html. – fiskolin Jan 06 '14 at 17:13
  • as long as $this->item->imageXLarge is actually set when you are assigning it to the global var..? – RaggaMuffin-420 Jan 06 '14 at 17:15
  • In my module, outside my `item.php` - the file that renders Image firstly. – fiskolin Jan 06 '14 at 17:17
  • You likely would not want to introduce globally scoped variables into your classes. This is poor programming practice. If OP can add class information to the question, better suggestions can be given on how to get this value. – Mike Brant Jan 06 '14 at 17:24
  • I dunno if we're at the same topic. :( I have **2** pages: **1)** the page where `$this` can be applied and **2)** another page, outside of first one where I need to access `$this` variable. – fiskolin Jan 06 '14 at 17:25
  • 2
    No no please do not introduce globally scoped variables. I don't know the k2 methods but what you want to do is get the id from a JInput object that you create in your variable and then se that probably with the static method. $this is not the the same thing in your module and in your article. – Elin Jan 06 '14 at 23:59