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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.