3 Replies - 329 Views - Last Post: 28 December 2017 - 06:35 AM Rate Topic: -----

#1 noobfred   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-December 17

Notice: Undefined property: login::$login

Posted 27 December 2017 - 03:51 AM

So I have tried quite some things but i keep getting the error:

Quote

Notice: Undefined property: login::$login ... on line 12

I get this this error when i try to create a test login class/object and show it like this:

<?php
class login {
  var $username;
  var $password;
  public function login($user, $pass) {
      $this->username = $user;
      $this->password = $pass;
  }
}

$test = new login('root', 'root');
echo($test->login);
?>


I really don't know how to fix this... :(/>/>

Is This A Good Question/Topic? 0
  • +

Replies To: Notice: Undefined property: login::$login

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2563
  • View blog
  • Posts: 10,281
  • Joined: 03-December 12

Re: Notice: Undefined property: login::$login

Posted 27 December 2017 - 06:36 AM

You are using PHP 4 style constructs.


Don't use var. Use the appropriate visibility construct.
Use __construct() rather than the class name.
Lastly, there is nothing to print. So, what are you trying to echo?


<?php
class Test {
  private $username;
  private $password;

  public function __construct($user, $pass) {
      $this->username = $user;
      $this->password = $pass;
  }

}

$test = new Test('root', 'root');



Was This Post Helpful? 1
  • +
  • -

#3 noobfred   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-December 17

Re: Notice: Undefined property: login::$login

Posted 28 December 2017 - 05:06 AM

Thanks!
I'm going to try the construct.
About the echo, I was trying to give an output of my $test, just to see if it was inserted correctly.

Cheers!
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2563
  • View blog
  • Posts: 10,281
  • Joined: 03-December 12

Re: Notice: Undefined property: login::$login

Posted 28 December 2017 - 06:35 AM

If you want to echo results out, create a new method and have the constructor call that method. Constructors shouldn't return anything.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1