what is $this

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
ahmed_mmm
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Sep 25, 2017 8:56 am

what is $this

Post by ahmed_mmm » Tue Nov 21, 2017 11:28 pm

Hi everyone
it's 1st time to me posting in joomla forum
normally i like to search for the answer which i'm seeking but for that question i didn't find anything .
days after days and can't figure out .
maybe this been asked a lot of time
what is $this ?
specially how does joomla use $this ?
i know $this is a reserved word in php
but how does joomla use $this outside the scope of class?
how do i know the referrer of $this


thanks
Last edited by imanickam on Wed Nov 22, 2017 2:41 am, edited 1 time in total.
Reason: Moved the topic from the forum General Questions/New to Joomla! 3.x to the forum Joomla! 3.x Coding

itoctopus
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4025
Joined: Mon Nov 25, 2013 4:35 pm
Location: Montreal, Canada
Contact:

Re: what is $this

Post by itoctopus » Thu Nov 23, 2017 1:31 pm

$this points to a method/attribute in the current class. If you have a PHP class, and you have a method called "subtract" for example, then, in order to use this method (function) in another method, then you will need to call $this->subtract.
http://www.itoctopus.com - Joomla consulting at its finest
https://twitter.com/itoctopus - Follow us on Twitter

ahmed_mmm
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Sep 25, 2017 8:56 am

Re: what is $this

Post by ahmed_mmm » Tue Nov 28, 2017 12:49 pm

Hi itoctopus

yes you are right if using it ($this) inside the scope of class
but for example at components\com_users\views\login\tmpl
there are three files (default, default_login, default_logout)
what is the class that $this refer to ?
Last edited by ahmed_mmm on Tue Nov 28, 2017 1:57 pm, edited 1 time in total.

Achaa
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 172
Joined: Mon Jul 29, 2013 8:25 pm

Re: what is $this

Post by Achaa » Tue Nov 28, 2017 1:22 pm

$this will reference the current instance (object) of the class you are using at any given time.

ahmed_mmm
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Sep 25, 2017 8:56 am

Re: what is $this

Post by ahmed_mmm » Tue Nov 28, 2017 1:54 pm

Hi Achaa
yes you are right
take this example and tell me what class or object $this refer to ?
it's the default.php in com_users

Code: Select all

defined('_JEXEC') or die;

$cookieLogin = $this->user->get('cookieLogin');

if (!empty($cookieLogin) || $this->user->get('guest'))
{
	// The user is not logged in or needs to provide a password.
	echo $this->loadTemplate('login');
}
else
{
	// The user is already logged in.
	echo $this->loadTemplate('logout');
}

Achaa
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 172
Joined: Mon Jul 29, 2013 8:25 pm

Re: what is $this

Post by Achaa » Tue Nov 28, 2017 2:39 pm

Hi,
I'm not familiar with Joomla code, but a quick Google leads me to JView.

https://docs.joomla.org/API17:JView::loadTemplate


Class page: https://docs.joomla.org/API17:JView

ahmed_mmm
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Sep 25, 2017 8:56 am

Re: what is $this

Post by ahmed_mmm » Wed Nov 29, 2017 1:13 am

Thanks
does JView::loadTemplate relate to this topic ?
anyway i think the loadTemplate method is deprecated
anyone knows how the joomla code been organized????
OR
how joomla uses $this in a multi file php ?

i think i'm not the only one who want to know answers for these questions .
please someone explain

Achaa
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 172
Joined: Mon Jul 29, 2013 8:25 pm

Re: what is $this

Post by Achaa » Wed Nov 29, 2017 7:36 am

I don't think you understand the concept of $this.
I would politely suggest you do some reading on Object-oriented programming.

ahmed_mmm
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Sep 25, 2017 8:56 am

Re: what is $this

Post by ahmed_mmm » Wed Nov 29, 2017 6:15 pm

Code: Select all

<?php
class Person {
    public $name;

    function __construct( $name ) {
        $this->name = $name;
    }
}
that's what i understand about $this
anything else you suggest to understand


Locked

Return to “Joomla! 3.x Coding”