Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is "this->" mean? 3

Status
Not open for further replies.

JohnnyT

Programmer
Jul 18, 2001
167
GB
Hi all,

I have found this line in a script and I'm wondering what it does.
$extra_vars = $this->someFunction();

How is this line different from just:
$extra_vars = someFunction();

I've tried searching for the answer on google but I'm getting nowhere.

I'd really appreciate it if someone could shed some light on this for me.

Many thanks

John ;-)



I don't make mistakes, I'm merely beta-testing life.
 
Its object oriented notation.

Its basically a way to call a method (function) that's part of an object.

"this" would refer to the object in which the current code is being run.

Code:
<?php

class Something {
  var $x;

  function setX($v) {
   $this->x=$v;
  }

  function getX() {
   return $this->x;
  }
}

?>







----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi Phil

Thanks for your reply. So, are they only used within a "class" then?

Sorry if this is a silly question but I'm still trying to get my head around them.

I can see from your explanation that $this->x is refering to the var $x that has been declared at the start of the class. But why not use global $x inside the other functions?

Thanks for your help

Cheers

John ;-)

I don't make mistakes, I'm merely beta-testing life.
 
You don't need global x because the variable exists within the scope of the object/class.

Its a property of the object. So it can be used by the object's functions.

You may want to look into object oriented tutorials to get the hang of it:














----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for the link Phil

Will read and (hopefully) digest!

Cheers

John ;-)

I don't make mistakes, I'm merely beta-testing life.
 
Phil

Just to let you know that I've read that excellent article and consequently understand far more about Classes and the this-> declaration now.

In fact.. if I'm right, your first example should have a function Something() in it that would be run initially when a new class was declared with $myVar = new Something();

The this-> makes perfect sense to me now.

Many thanks for pointing me in the right direction, it is much appreciated.

Cheers

John ;-)

I don't make mistakes, I'm merely beta-testing life.
 
whilst that would work, JohnnyT, it is not advisable in php5. calling a method the same name as the class was for php4 only. php5 allows it for backward compatibility.

It is better to use a magic method instead

Code:
class foo{
  public method __construct(){
     $this->me = 'bar';
  }
  public method bar(){
   echo $this->me;
  }
}
 
jpadie,

Thanks for the information. Glad you said before I got into a bad habit.

Thanks again

Cheers

John ;-)

I don't make mistakes, I'm merely beta-testing life.
 
Sorry I did not get back tot his. But yeah jpadie is as usual right on the money.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Well thanks to both of you for all your help.

Coincidentally enough my new found knowledge of classes really helped me out with some fault finding today. ;-)

Thanks again

John

I don't make mistakes, I'm merely beta-testing life.
 
The problem with the explanations is that they are written by people who know what they are talking about for people who are clueless.

Let me take a stab at this:

I think the English for this is:
class something The name of a template or pattern which will contain both things and actions


{
var $thing some object included in the concept
var $whatzit some quality included in the concept
var $chomp object, action or property defined in method statement
var $attitude = "hungry and cranky"
function Survival_of_The_Fittest() an action that will be applied when you use the template.

{
$this->thing tells the gremlins that make PHP work that thing is part of the code and can be used in it.
$this->whatzit = "triffid" Hey, gremlins, whatzits is part of this too. they are triffids.
$this->chomp() by the way, gremlins, chomping can happpen here.
$this->Heffalump}
function __Construct(BattleoftriffidsandHeffalllumps)
{I'm too tired to figure out who would eat who..if else, but here is where it happens. something like
$this->Heffalump =$thisHefalump++
$this->Whatzit = $this->Attitude
Echo $This->Chomp = "Euueww";
}
}


Now I think, and that's after confusing myself unduly with about five books, that the $this-> statements are sort of a list of characters. they can't play if they aren't signed in. If you don't have a nametag, you can't speak at the podium. Sometimes your nametag tells people what you do, but it's at least your entry pass to the room.

That's surely not all right, but I think it's the gist. My question is do you need both the variables and the $this-> statements, and do they need to be identical? (I know the $this statements are necessary.)

The confusing thing about the $this-> construction is order. It is not at the beginning, as one would expect. Classes are first read through by php before they are acted on (I read this, so it's not my idea if it's perfectly wrong) so following them logically requires some flexibility.


Thanks for the question.


 
jlockley

you may be getting yourself turned around.

you are right that most explanations on this board are written by people who have some knowledge. But we do not write them for people who are clueless. They are designed for people with at least basic IT knowledge. not necessarily enough to understand the explanation off the bat, but enough to take that info and drill down into it until they hit a blocking point (at which point they follow up with another question or go investigate themselves).

This board is different to others in that membership is only open to IT Professionals. Whilst there is no hard definition of this term, loosely speaking my paragraph above describes how I understand the term.

to understand the $this construct you need first to understand classes. There is no point in trying to get to grips with classes and objects until you have first got the building blocks of the language under your belt. For this I would start by investigating the language in this order. Do this by reading the php manual and using a code editor to try things out yourself. tutorials may help you later, now it's time to read and do things yourself.

1. the types of construct in php (variables, constants, functions, 'keywords').
2. php is a loosely typed language (after point 1 you will know what this means); but arrays are not simple variables. go learn a lot about arrays. you will use them the whole time. really learn about comparison operators and the difference between =, == and ===
3. learn about keeping code that you want to reuse inside functions. learn how to use a function: when to have the function 'do' something and when to have it return a piece of processed information for you to use elsewhere.
4. learn about the difference between procedural code and object code. Don't necessarily try to handle objects yet, just learn that the difference exists, how to recognise it and what the relative advantages and disadvantages are.
5. learn about how php can communicate with external sources - such as files, universal resources, databases, query strings, html forms.
6. try writing a small application now. perhaps a login manager. this will test your ability to use comparison operators, external data sources and reusable code snips in functions.
7. once you've done 6, search these forums for login managers and compare the code we have published in the past with yours. analyse where yours is better and where it is deficient.
8. now is the time to tackle objects. read the php manual on classes and objects really carefully a few times. they are 'new things' to people who grew up in Basic, VB and other procedural languages. the concepts of encapsulation and inheritance are really important to understand.
9. try rewriting your login manager as an object.
 
you are right that most explanations on this board are written by people who have some knowledge. But we do not write them for people who are clueless. They are designed for people with at least basic IT knowledge

I did not mean you. I was in essence referring to the various books around, which often make assumptions which increase the challenge.

I've got points 1 - 9 down, more or less, but turned around? oop is doing it pretty well. Arrays? Brilliant! Functions? Enormously practical! Keeping repeated tasks in distinct files to be called up when needed? Love it!
Oops? Still waiting for the aha. The theory is clear, but the procedure confounds.

As usual, thanks.





 
it took me a year or so. look back through the posts here and for a long time i sounded off against OOP. preferring to code even the most complex app in procedural code.

i still maintain that there is very little that is doable in OOP that cannot be done in procedural code, but having been forced into OOP by the need to use PDO for a project, i now am fully bought into the usefulness of classes.

i am now in a quandary about namespaces. for me their main usefulness is already present in classes. so why should i use a namespace over an object? i've yet to find a decent rationale but i applaud the php core team for evolving php closer and closer to a mainstream compiled language (albeit without real compilation yet!).
 
I've finally found a couple of books which make it clearer..thank you very much. O'Reilly, of course, and Ullman. I am not about to start writing it, but I recognize and understand it when confronted with it.

I have substantial plans for an otherwise static page system, and neither the funds nor the patience to let anyone else do it. (control freak, really). Thanks so much for the occasional push in the right direction.
 
take a look at wordPress. it will probably do everything you need right off the bat. do not get het up about the fact that it is a blog engine. it can be easily made to do anything you need.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top