Jump to content


Photo

Questions about an arg in a method


  • Please log in to reply
4 replies to this topic

#1 eldan88

eldan88

    Advanced Member

  • Members
  • PipPipPip
  • 477 posts
  • LocationNew York

Posted 21 February 2014 - 04:09 PM

Hey Guys. I came across a method that had an argument without  a dollar sign next to it. (Talk)

 

I was just wondering how that works since I never seen it before. Below is an  example

 

Thanks your help in advance!

class Speaker {

protected $talk; 

public function setTalk(Talk $talk) { // No dollar sign on the first argument for.. Not sure
what it is used for?

}

$this->talk =  $talk;

public function getTalkTitle {

return $this->talk-getTitie(); 

}

}


#2 WebStyles

WebStyles

    Advanced Member

  • Members
  • PipPipPip
  • 1,215 posts
  • LocationEurope

Posted 21 February 2014 - 04:15 PM

Type hinting. Check out: http://www.php.net/m...typehinting.php


Need a Web Developer? Check out ClickWorks.org | I work on a PAY-WHAT-YOU-WANT basis (because the world needs a change)

 

"If it looks like a duck, and quacks like a duck, we have at least to consider the possibility that we have a small aquatic bird of the family anatidae on our hands!"

-- Douglas Adams


#3 Rifts

Rifts

    Warlock

  • Members
  • PipPipPip
  • 713 posts
  • LocationAZ
  • Age:27

Posted 21 February 2014 - 09:00 PM

its creating an instance of the class Talk and storing into a new variable called $talk. the "Talk" isn't an arg.


Edited by Rifts, 21 February 2014 - 09:00 PM.


#4 eldan88

eldan88

    Advanced Member

  • Members
  • PipPipPip
  • 477 posts
  • LocationNew York

Posted 22 February 2014 - 04:38 PM

Hey Guys. Thanks for your response. I did come across a tutorial on youtube about type hinting. I sort of understand it now. The one one thing I don't understand is why is there a need to use the construct function on the test class, what does it exactly construct?? Below is the code. Thanks for your help in advance. 

<?php 

class Test { 

public function __construct(){


}

public function Write() {
	echo "I am writing from Test";
}



} // end  of class test

class Foo {

public function __construct(Test $a) {

$this->Newobj = $a; 
$this->Newobj->Write();

}


}




 new foo(new Test);




?>


#5 KevinM1

KevinM1

    Snarkimus Prime

  • Moderators
  • 5,196 posts
  • LocationNew Hampshire, USA

Posted 22 February 2014 - 05:24 PM

Every object has a constructor.  Test's explicitly empty constructor is a bit redundant since if you try instantiating a class that doesn't have a constructor, PHP's general purpose default constructor will execute instead.  A working example of that in action:
 
class Example
{
    public function blah()
    {
      echo "blah";
    }
}
    
$x = new Example();
$x->blah(); // echoes "blah"
Notice that new Example still worked even though I didn't write a constructor.

Foo's constructor is written so that it will only accept an object of type Test as an argument.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com