Hi,

I am learning about how to write OOP PHP code but I keep coming across lines such as

$this -> $sender = $sender

I'd really appreciate it if anyone here could clue me in on this as I can't get any info on the web about it.


Thanks in advance.

sets keys to values in an array ex.

$array = array("mydog"->"spot", "yourdog"->"rover");

it is also used in this syntax

$object->function();

or

$object->variable = $variable;

where "function()" would be a function in the instantiated object
and "variable" would be a variable in the instantiated object.
I'm not exactly sure what that is that you have listed there though, if you took out the second "$" it would make sense.

sets keys to values in an array ex.

$array = array("mydog"->"spot", "yourdog"->"rover");

it is also used in this syntax

$object->function();

or

$object->variable = $variable;

where "function()" would be a function in the instantiated object
and "variable" would be a variable in the instantiated object.
I'm not exactly sure what that is that you have listed there though, if you took out the second "$" it would make sense.

Hi R0bb0b,

First of all the extra $ is my fault. I apologise.

OK I get what the purpose of "->" is now, good explanation, thank you very much.

If you could would you be able to give me a quick explanation of the $this keyword. I take it the code below is just saying that the variable subject in the public function is = the value that is passed when the function is called.

public function setSubject($subject)
  {
    $this->subject = $subject;
  }

So does it mean that this version is the same and also valid:

public function setSubject($subject)
  {
    setSubject->subject = $subject;
  }

Many thanks again.

I've never seen it done that way and actually its not done to reference the parenting function but rather $this refers to the parenting class. Here's how it works.

If you know a little about OOP then you should get this.

class One
{
	var $classonevar = "";
	function __construct() //constructor, can also be called function One() for previous PHP versions
	{
		
	}
	
	function setClassOneVar($input)
	{
		$this->classonevar = $input;
		$this->classtwovar = $input; //doesn't work because it is out of scope
	}
}

class Two extends One
{
	var $classtwovar = "";
	function __construct() //constructor, can also be called function Two() for previous PHP versions
	{
		
	}
	
	function setClassOneVar($input) //overrides One.setClassOneVar
	{
		$result = parent::setClassOneVar("dog"); //calls the setClassOneVar in class One
		
		//$result can then be manipulated here and returned from here
		
		$this->classonevar = $input; //also manipulates $classonevar because this class extends that class
		$this->classtwovar = $input; //manipulates $classtwovar in this class
	}
	
	function setClassTwoVar($input)
	{
		$this->classtwovar = $input; //manipulates $classtwovar in this class
	}
}

$obj1 = new One();
$obj1->//any function or public variable in class One
$obj2 = new Two();
$obj2->//any function or public/protected variable in class Two or One
Comments
Great help. Explanations and code. Appreciate it.

R0bb0b,

A big thanks. I really appreciate the time taken.

I'm working my way through Object Oriented Programming with PHP5 by Hasin Hayder, but it isn't very clear in places. And the number of grammatical errors is astounding. Duplicated lines as well. I think I may get another book on the subject.

Anyway thanks for the help.

As many studies in OOP as I had through school and even after school, didn't really do me any good either. It wasn't until about 2 yrs into my career that I actually learned the value of inheritance and function overriding. So I would say, don't worry, it will come to you.

One good example would be if you have an application that you will need to update in the future, you don't want to actually modify the code in the application itself because once you run the next update, whatever modifications you have made would now be broken. Rather you would want to make classes that extend classes in said application so when the application is updated, your code doesn't break but may have to be modified a little.

Comments
Very helpful

Im sorry, I gave you the incorrect array syntax
$array = array("mydog"->"spot", "yourdog"->"rover");
would actually be
$array = array("mydog"=>"spot", "yourdog"=>"rover");

This question has already been answered. Start a new discussion instead.