Jump to content

PHP Class problem


satal keto

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.