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 Link to comment https://forums.phpfreaks.com/topic/41258-php-class-problem/ 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 " <-"; ?> Link to comment https://forums.phpfreaks.com/topic/41258-php-class-problem/#findComment-199905 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. Link to comment https://forums.phpfreaks.com/topic/41258-php-class-problem/#findComment-199908 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? Link to comment https://forums.phpfreaks.com/topic/41258-php-class-problem/#findComment-199912 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 $! Link to comment https://forums.phpfreaks.com/topic/41258-php-class-problem/#findComment-199927 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. Link to comment https://forums.phpfreaks.com/topic/41258-php-class-problem/#findComment-199929 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. Link to comment https://forums.phpfreaks.com/topic/41258-php-class-problem/#findComment-199943 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.