satal keto Posted March 5, 2007 Share Posted March 5, 2007 I am trying to learn how to do OOP in PHP. But I am having a slight problem with my code I am using the following code <?php class HAPI { var $Login; var $Pass; var $Hapikey; var $Auth; function SetLogin($value) { $this->$Login = $value; } function SetPass($value) { $this->$Pass = $value; } function SetAuthKey($value) { $this->$Authkey = $value; } } $hyp = new HAPI(); echo "1<br>"; $hyp->SetLogin("Username"); echo "2<br>Login: "; echo $hyp->Login; echo " <-"; ?> For some reason this isn't doing anything. It is printing out 1 2 Login: <- Does anyone have any idea on what I am doing wrong? Thanks for any help in advance. Regards Satal Quote Link to comment Share on other sites More sharing options...
mbtaylor Posted March 5, 2007 Share Posted March 5, 2007 You are accessing your properties wrong. It should be like this: <?php class HAPI{ var $Login; var $Pass; var $Hapikey; var $Auth; function SetLogin($value){ $this->Login = $value; } function SetPass($value){ $this->Pass = $value; } function SetAuthKey($value){ $this->Authkey = $value; } } $hyp = new HAPI(); echo "1<br>"; $hyp->SetLogin("Username"); echo "2<br>Login: "; echo $hyp->Login; echo " <-"; ?> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 5, 2007 Share Posted March 5, 2007 Try this: <?php class HAPI { var $Login; var $Pass; var $Hapikey; var $Auth; function SetLogin($value) { $this->Login = $value; } function SetPass($value) { $this->Pass = $value; } function SetAuthKey($value) { $this->Authkey = $value; } } $hyp = new HAPI(); echo "1<br>"; $hyp->SetLogin("Username"); echo "2<br>Login: "; echo $hyp->Login; echo " <-"; ?> When you set the value of each variable you did it like this: $this->$Login = $value; I don't think your supposed to have that extra $ sign before the Login. So I put it to this: $this->Login = $value; I did that to all of them, so try copy and pasting the code I gave you above and see what happens. Quote Link to comment Share on other sites More sharing options...
r-it Posted March 5, 2007 Share Posted March 5, 2007 sorry guys but my eyes must be deceiving me but isnt that exactly the same code, with just the left brace changing position? Quote Link to comment Share on other sites More sharing options...
mbtaylor Posted March 5, 2007 Share Posted March 5, 2007 We put $this->Login = $value; You put $this->$Login = $value; NOTE THE $! Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 5, 2007 Share Posted March 5, 2007 Look, we changed this: <?php function SetLogin($value) { $this->$Login = $value; } function SetPass($value) { $this->$Pass = $value; } function SetAuthKey($value) { $this->$Authkey = $value; } ?> To: <?php function SetLogin($value) { $this->Login = $value; } function SetPass($value) { $this->Pass = $value; } function SetAuthKey($value) { $this->Authkey = $value; } ?> Here is what was specifiacally changed. $this->$Login = $value; to --> $this->Login = $value; $this->$Pass = $value; to --> $this->Pass = $value; $this->$Authkey = $value; to --> $this->Authkey = $value; Did you try to code to see if it worked? mbtaylor - You keep beating me to it by just a hair, hah. Quote Link to comment Share on other sites More sharing options...
mbtaylor Posted March 5, 2007 Share Posted March 5, 2007 mbtaylor - You keep beating me to it by just a hair, hah. 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.