salman_ahad@yahoo.com Posted October 27, 2009 Share Posted October 27, 2009 This is my code. I have three strings in MyFirstClass. I am trying to call them in MySecondClass <?php class MyFirstClass{ private $string1="Hello",$string2="He",$string3="Hi"; function getString1(){ return $this->string1; } function getString2(){ return $this->string2; } function getString3(){ return $this->string3; } } class MySecondClass{ private $var1,$var2,$var3; __Construct(MyFirstClass $firstClass){ getFirstClassVars($firstClass); printFirstClassVars(); } function getFirstClassVars($firstClass){ $this->var1 = $firstClass->getString1(); $this->var2 = $firstClass->getString2(); $this->var3 = $firstClass->getString3(); } function printFirstClassVars(){ print $this->var1."<br/>"; print $this->var2."<br/>"; print $this->var3."<br/>"; } } $firstClass = new MyFirstClass(); $secondClass = new MySecondClass($firstClass); ?> I am getting this error "syntax error, unexpected T_STRING, expecting T_FUNCTION" at this line __Construct(MyFirstClass $firstClass){ Quote Link to comment https://forums.phpfreaks.com/topic/179238-solved-expecting-t_string-error/ Share on other sites More sharing options...
Alex Posted October 27, 2009 Share Posted October 27, 2009 It should be function construct(...) Quote Link to comment https://forums.phpfreaks.com/topic/179238-solved-expecting-t_string-error/#findComment-945659 Share on other sites More sharing options...
Mchl Posted October 27, 2009 Share Posted October 27, 2009 public function __construct() to be exact Quote Link to comment https://forums.phpfreaks.com/topic/179238-solved-expecting-t_string-error/#findComment-945661 Share on other sites More sharing options...
Alex Posted October 27, 2009 Share Posted October 27, 2009 public function __construct() to be exact Depending on the version of PHP. Quote Link to comment https://forums.phpfreaks.com/topic/179238-solved-expecting-t_string-error/#findComment-945662 Share on other sites More sharing options...
Mchl Posted October 27, 2009 Share Posted October 27, 2009 Unless stated otherwise all my statements refer to PHP 5+ Quote Link to comment https://forums.phpfreaks.com/topic/179238-solved-expecting-t_string-error/#findComment-945664 Share on other sites More sharing options...
Daniel0 Posted October 27, 2009 Share Posted October 27, 2009 public function __construct() to be exact Depending on the version of PHP. No. I assume you're talking about PHP 4's lack of visibility keywords, but in PHP 4 the constructor was not called __construct(). public function __construct() to be exact Actually, public is optional for methods because things by default are public. Quote Link to comment https://forums.phpfreaks.com/topic/179238-solved-expecting-t_string-error/#findComment-945665 Share on other sites More sharing options...
Mchl Posted October 27, 2009 Share Posted October 27, 2009 I don't like to rely on defaults. It's not like adding six more letters slows the script or makes me work twice as long. Quote Link to comment https://forums.phpfreaks.com/topic/179238-solved-expecting-t_string-error/#findComment-945669 Share on other sites More sharing options...
Daniel0 Posted October 27, 2009 Share Posted October 27, 2009 Yeah I'd agree it's better practice, but strictly speaking it's not required. Quote Link to comment https://forums.phpfreaks.com/topic/179238-solved-expecting-t_string-error/#findComment-945670 Share on other sites More sharing options...
Alex Posted October 27, 2009 Share Posted October 27, 2009 public function __construct() to be exact Depending on the version of PHP. No. I assume you're talking about PHP 4's lack of visibility keywords, but in PHP 4 the constructor was not called __construct(). public function __construct() to be exact Actually, public is optional for methods because things by default are public. Yes, I was referring to that. But I forgot that constructors in PHP 4 were the same name as the class. Doh. Quote Link to comment https://forums.phpfreaks.com/topic/179238-solved-expecting-t_string-error/#findComment-945673 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 27, 2009 Author Share Posted October 27, 2009 Are you guys helping or confusing... thanks though.. let me try Quote Link to comment https://forums.phpfreaks.com/topic/179238-solved-expecting-t_string-error/#findComment-945688 Share on other sites More sharing options...
salathe Posted October 27, 2009 Share Posted October 27, 2009 You need to change: __Construct(MyFirstClass $firstClass){ getFirstClassVars($firstClass); printFirstClassVars(); } to: public function __construct(MyFirstClass $firstClass){ $this->getFirstClassVars($firstClass); $this->printFirstClassVars(); } If you don't know why, just ask. Quote Link to comment https://forums.phpfreaks.com/topic/179238-solved-expecting-t_string-error/#findComment-945693 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 27, 2009 Author Share Posted October 27, 2009 Thanks Salate and all, it worked. Now I need to separate this classes as two php, MyFirstClass.php and MySecondClass.php. Save them in the same folder and try. If there is any other slight change I need to make. Please help. Quote Link to comment https://forums.phpfreaks.com/topic/179238-solved-expecting-t_string-error/#findComment-945704 Share on other sites More sharing options...
Daniel0 Posted October 27, 2009 Share Posted October 27, 2009 Please help. With what? Quote Link to comment https://forums.phpfreaks.com/topic/179238-solved-expecting-t_string-error/#findComment-945714 Share on other sites More sharing options...
Mchl Posted October 27, 2009 Share Posted October 27, 2009 One thing that comes to mind, is that you will need to include first class before you can use it with second. Quote Link to comment https://forums.phpfreaks.com/topic/179238-solved-expecting-t_string-error/#findComment-945716 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 27, 2009 Author Share Posted October 27, 2009 Thanks Guys...it worked. I will try include() too... My first php class is test1.php <?php class MyFirstClass{ private $string1="Hello",$string2="Testing",$string3="Hi"; function getString1(){ return $this->string1; } function getString2(){ return $this->string2; } function getString3(){ return $this->string3; } } ?> My second php class is test2.php <?php require('test1.php'); class MySecondClass{ private $var1,$var2,$var3; public function __construct(MyFirstClass $firstClass){ $this->getFirstClassVars($firstClass); $this->printFirstClassVars(); } function getFirstClassVars($firstClass){ $this->var1 = $firstClass->getString1(); $this->var2 = $firstClass->getString2(); $this->var3 = $firstClass->getString3(); } function printFirstClassVars(){ print $this->var1."<br/>"; print $this->var2."<br/>"; print $this->var3."<br/>"; } } $firstClass = new MyFirstClass(); $secondClass = new MySecondClass($firstClass); ?> And this is what I needed. It worked. Quote Link to comment https://forums.phpfreaks.com/topic/179238-solved-expecting-t_string-error/#findComment-945718 Share on other sites More sharing options...
Mchl Posted October 27, 2009 Share Posted October 27, 2009 You used require which does almost the same thing as include except it throws an error when required file can not be found. It is fine as it is. Quote Link to comment https://forums.phpfreaks.com/topic/179238-solved-expecting-t_string-error/#findComment-945720 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.