Jump to content


Questions about an arg in a method

  • Please log in to reply
4 replies to this topic

#1 eldan88

  • Members
  • PipPipPip
  • Advanced Member
  • 492 posts
  • LocationNew York

Posted 21 February 2014 - 09: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

  • Members
  • PipPipPip
  • Advanced Member
  • 1,215 posts
  • LocationEurope

Posted 21 February 2014 - 09: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

  • Members
  • PipPipPip
  • Warlock
  • 726 posts
  • LocationAZ
  • Age:27

Posted 22 February 2014 - 02:00 AM

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, 22 February 2014 - 02:00 AM.

#4 eldan88

  • Members
  • PipPipPip
  • Advanced Member
  • 492 posts
  • LocationNew York

Posted 22 February 2014 - 09: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. 


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; 



 new foo(new Test);


#5 KevinM1

  • Moderators
  • Snarkimus Prime
  • 5,248 posts
  • LocationNew Hampshire, USA

Posted 22 February 2014 - 10: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.
Using 'global' is a sign of doing it wrong

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users