Jump to content

php class problem


pouncer

Recommended Posts

[code=php:0]
class Profile_Details {
  var $username;
  var $sql;
  var $row;

  // Constructor.
  function Profile_Details($uname) {
  $this->username = $uname;
  $userid = $_SESSION['UserID'];
  $sql = mysql_query("SELECT * FROM user_profile WHERE user_id='$userid'");
  $row = mysql_fetch_assoc($sql);

  echo $row['Forename']; //this line echos correctly
  }

  function Get_Name() {
  return "gfgs" . $row['Forename'];
  }

[/code]

but the on the other page when i do

<?php echo $profile->Get_Name(); ?>

it just echos gfgs

and not the name after it, anyone see where im going wrong
Link to comment
https://forums.phpfreaks.com/topic/35236-php-class-problem/
Share on other sites

thanks jesirose. this worked

class Profile_Details {
var $username;
var $sql;
var $row;

// Constructor.
function Profile_Details($uname) {
$this->username = $uname;
$userid = $_SESSION['UserID'];
$this->sql = mysql_query("SELECT * FROM user_profile WHERE user_id='$userid'");
$this->row = mysql_fetch_assoc($this->sql);
}

function Get_Name() {
return "gfgs" . $this->row['Forename'];
}
}
Link to comment
https://forums.phpfreaks.com/topic/35236-php-class-problem/#findComment-166433
Share on other sites

hm, but ive got lots of functions like this..

function Get_Name() {
return $this->row['Forename'];
}

function Get_Surname() {
return $this->row['Surname'];
}

function Get_Age() {
return $this->row['Age'];
}

function Get_Gender() {
return $this->row['Gender'];
}

function Get_Location() {
return $this->row['Location'];
}

should i put those all in the constructor?
Link to comment
https://forums.phpfreaks.com/topic/35236-php-class-problem/#findComment-166440
Share on other sites

That's what I would do.
Think of the class as a blueprint.

Every user has an age, gender, name - those should all be vars for it.
var $age; var $gender;

Plus, that's less for you to write. $this->age is cleaner than $this->row['age'], right?

Redbull: you're right, they are valid but it doesn't follow what I've been taught about OOP design - so I'm just trying to explain how I've always done classes, the way my professors and boss showed me. I could be wrong. *shrug*
Link to comment
https://forums.phpfreaks.com/topic/35236-php-class-problem/#findComment-166443
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.