Jump to content

Recommended Posts

Hello everyone,

 

I have recently learned programming using OOP, i have done all those "hello world" applications and now have tried to use OOP in the real time programming.

 

In a simple script which gets Username from the database using the employee id. i am not able to use the mysql_fetch_assoc function.

here is my code

 

<?php 
require ("config.php");
session_start();


class person {

private $name;
private $id;
public function __construct()
  {
  $sqname = mysql_query("SELECT name FROM `users` WHERE `id` = '$id'");
  $assoc = mysql_fetch_assoc($sqname);
  $fname = $assoc['name'];
  $this->id = $_SESSION['loggedin'];
  $this->name = $fname;
  }
function get_name() {

return $this->name;
		}

}
?>

 

and in the view file:

 

<?php
include("classes/name.php");

$masood = new person;
echo $masood->get_name();
?>

 

and also let me know if i am doing something worng.

Link to comment
https://forums.phpfreaks.com/topic/226343-mysql_fetch_assoc-problem-in-php-oop/
Share on other sites

You can't use $id if it's undefined.  Look at where you attempt to use it in comparison to where you assign a value to it.

 

Thanks for the reply, :)

 

I tried doing that but it is of no use, and i also figured out i am not able to use sessions inside classes at all.

 

i tried to modify the script to show simple session, here it is:

session_start();
class person {
private $id;
public function __construct()
{

	$this->id = $_SESSION['id'];
}
function get_name()
{
	return $this->id;
	}
}

 

but i am not able to see anything on the page.

 

where as the same script works without session.

i am also able to echo the same session without using OOP method.

 

Knowing OOP syntax is only half the battle.  You need to understand the philosophy behind OOP to figure out how to structure things so they work properly.

 

One of the main tenets of OOP is encapsulation - that objects are self-contained entities which have the potential to work in different contexts.  Having an object look for a particular session variable breaks encapsulation because you're tying (coupling) the object to a particular system.  It won't be able to work unless that session variable exists.  Logically, your object shouldn't really care where its data comes from.  So long as it has legit data, it should be able to function correctly.  Use the constructor's argument list to pass in the correct values:

 

class Person
{
   private $id;
   private $name;

   public function __construct($id, $name = null)
   {
      $this->id = $id;
      $this->name = $name;
   }

   public function getName()
   {
      return $this->name;
   }
}

 

Now, what about constructing a person?  I like to use a factory for something like this, as, really, a Person should represent a single person's data and the actions which can be performed on that data.  Finding and constructing a new Person seems like a separate responsibility to me.  So, take a look at this bare-bones Factory class:

 

class PersonFactory
{
   // db config info/settings/variables

   public static function findPerson($id)
   {
      $query = mysql_query("SELECT name FROM users WHERE id = $id");
      $result = mysql_fetch_assoc($query);
      $person = new Person($id, $result['name']);

      return $person;
   }
}

 

Now, it's as simple as:

 

session_start();

$person = PersonFactory::findPerson($_SESSION['loggedin']);

 

You've now decoupled the Person from both sessions and the db.  It can act, and be acted upon, in an individual and free manner.  Keep in mind, this is a very rough sketch of how I would approach your problem.  There's no error checking, autoloading, etc.  Don't expect to be able to simply copy and paste this and have it work completely.

Hi guru, thank so much for the help,

i have sorted the problem out i donno how but i somehow did :P lol

 

here is the code which worked

session_start();

class person {
private $id;
private $name;


	 public function __construct()
	 {
		 $this->id = $_SESSION['loggedin'];
$employe_name = mysql_query("SELECT name FROM `users` WHERE `id` = '$this->id'");
$name = mysql_fetch_assoc($employe_name);
$emp_name = $name['name'];

$this->name = $emp_name;
	 }
	 function getName() {
		 return $this->name;
		 }

}

 

however i am now trying to learn more about encapsulation. i tried tweaking and playing with the script you suggested me but i am not able to understand some part of it

 

session_start();

$person = PersonFactory::findPerson($_SESSION['loggedin']);

not able to find out where and how to use this. i tired to echo it and also tried to put this in a separate class. but i always get a string error or a blank page. perhaps i need to understand this more.

 

Can u please suggest me some tutorials where i can learn about encapsulation, or could suggest me some scripts similar to this,  i learn fast by reverse engineering.

 

Thanks in advance :)

$person = PersonFactory::findPerson($_SESSION['loggedin']);

 

Is supposed to be used in your main script, or in which ever method needs to find a Person.  It's a static method, so it's able to be invoked without needing an actual instantiated object.

 

For resources, there are two:

 

1. http://www.amazon.com/Objects-Patterns-Practice-Experts-Source/dp/143022925X/ref=sr_1_1?ie=UTF8&qid=1296674773&sr=8-1

 

2. http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=sr_1_1?s=books&ie=UTF8&qid=1296674826&sr=1-1

 

They're the two best.  Read #1, then #2.  Free tutorials tend to be shoddy, and many teach bad habits.  You may as well pay some money to learn from the best sources possible if you want to do it right the first time.

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.