eldan88 Posted February 21, 2014 Share Posted February 21, 2014 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(); } } Quote Link to comment Share on other sites More sharing options...
WebStyles Posted February 21, 2014 Share Posted February 21, 2014 Type hinting. Check out: http://www.php.net/manual/en/language.oop5.typehinting.php Quote Link to comment Share on other sites More sharing options...
Rifts Posted February 22, 2014 Share Posted February 22, 2014 (edited) its creating an instance of the class Talk and storing into a new variable called $talk. the "Talk" isn't an arg. Edited February 22, 2014 by Rifts Quote Link to comment Share on other sites More sharing options...
eldan88 Posted February 22, 2014 Author Share Posted February 22, 2014 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); ?> Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted February 22, 2014 Share Posted February 22, 2014 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.