Jump to content

meaning of $this->name = $new_name; ?


fdrouillard

Recommended Posts

New to PHP and very new to OOP, and feel the need to ask a basic question.

 

What exactly is going on in the following statement:

 

$this->name = $new_name;

 

I know what the right side of the equation is, but not the left side. Why can't we simply use the variable (or property) $new_name?

 

Like I said, I'm new to PHP and very new to OOP.

 

Thanks in advance to anyone with the patience to answer what is likely a ridiculously simply question.

Link to comment
Share on other sites

Hi,

 

Welcome to the forum.

 

The '$this' is a special built-in self referencing variable, used in object oriented php .. it basically tells php to look for, or use the 'name' variable (in your example) of the current object. This is probably confusing because it is hard to understand without the big picture ... have you done my tutorial on OOP and PHP?

 

Let me know,

 

Stefan

Link to comment
Share on other sites

If I understand it correctly, $this->name is the value of the name property of the object $this, and the variable $new_name. So information is flowing from left to right, or from the object to the new variable.

 

I think I'm getting it.

 

Thank you for the reply. I've started your tutorial and find it very helpful.

Link to comment
Share on other sites

If I understand it correctly, $this->name is the value of the name property of the object $this

I think you are understanding this correctly. I believe "name" (in $this->name) would be called a variable, not a property though.

 

So information is flowing from left to right, or from the object to the new variable.

Not quite. This line sets the "name" variable of the object ($this->name) to the same value as $new_name.

 

$new_name = "Ben";

$this->name = $new_name;

 

After these two lines, $this->name would equal "Ben". Make sense?

Link to comment
Share on other sites

$this is not required but seen as a good practise to call on class wide or function wide variables within a class. It makes reading the code a lot smoother as you quickly can see and understand the code.

 

so it's basically like this:

 

$new_name = "Ben";

$this->name = $new_name;

 

$this->name is in other words a variable called $name within that class. So assigning $new_name's value to $name is what happened there.

 

Now for you to understand it better let's assume you have a class called Persons for let say a phone book, and you use the class Person to store details about these persons. So you would preferably want to store a new person, and access its details.

 


Class Persons {

private $name;    // Variable that can only be accessed from within the Person class.
private $phoneNbr;  // -||-

Function __Construct($personName, $personPhNbr ) {
    $this->name = $personName;  // Assign the value from the attribute to the class variable $name
    $this->phoneNbr = $personPhNbr;

}

Public Function getName() {
    return $this->name;
}

 Public Function getNbr() {
    return $this-phoneNbr;
}

}

// add a person by creating a new object
  $p = new Person("John", "1234");

// displaying the name of the person stored in the object $p is reffering to
  echo $p->getName();

// displaying the phone number of the person stored in the object $p is reffering to
  echo $p->getNbr();

 

Now you have a nice way of adding persons and getting the info required. So you could now create an array where each element points to a Person class, and you have a nice organised and clean code.

 

the "arrow" between the variable is just a pointer like in most OOP languages, in java it would be a dot but PHP have chosen to go with the arrow instead. In simple terms it says;

 

$p which is pointing/reffering to an object of type Person, Access the function with that name in that object.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...