Jump to content
Mudsaf

Few question about OOP & Database fetching result

Recommended Posts

So currently learning a bit OOP by myself. Made this script with little knowledge i have about OOP.  I tried changing public function __construct() to private/secure, but it gave me error and struggled to find real solutions how to do it as secured or private.

<?php
session_start();

class AccountInformation {
	public function __construct() {
		//Fetch account information, prevent premission for start
		
		if (isset($_SESSION['username'])) {
		
		if (!isset($con)) {
		include "inc/con.php";	
		}
		
			if ($res_fai = $con->prepare("SELECT firstname FROM tb_accounts WHERE username = ? LIMIT 1")) {
			$res_fai->bind_param("s", $_SESSION['username']);
			
				if ($res_fai->execute()) {
				$res_fai->bind_result($acc_fname);
				$res_fai->fetch();
				
				$this->fname = $acc_fname;
				}
			}
		
		} else {
			$this->fname = null;
		}
	}
	
}

$acc_data = new AccountInformation;

//echo $acc_data->fname;
//var_dump($acc_data);
?>

So echo and dump works perfectly fine, but i'm wondering if theres anything i could do better? Security/Management-wise. My own script is slightly different, but this runs the same idea that i have (just getting more data from db). 

I also tried to to set fname like this below, but got error (assuming i cant set private "variables" inside public function

private $fname = $acc_fname;

 

Edited by Mudsaf

Share this post


Link to post
Share on other sites

The constructor always needs to be public.  If you want properties to be private you need to declare them as private first.  It's common to declare all of the properties before the methods.

class AccountInformation {

  private $fname;

  private $connected = false; // you can initialize variables but only to scalar values, you can't initialize a property to another variable

  public function __construct($fname = '') {

    $this->fname = $fname; // you can only access a private property from inside this class

    $this->connect(); // you can only call a private method from inside this class
  }

  private function connect() {

  }

}

Have you always been in Finland?  My wife lived there for 5 years.

  • Like 1

Share this post


Link to post
Share on other sites

Thanks for the help, very much appreciated! Yes i have lived here all my life 😃

Share this post


Link to post
Share on other sites

Encountered another problem, i edited my code alot and not sure how to access the data properly.

var_dump($object->fetchUD("testuser"));

This returns all data of the user, but lets say i need to access the testuser email, tried below.

//var_dump($object->fetchUD("testuser")->email);

Basically my class has protected properties predefined (like below)

class AccountInformation extends ConnectDB {
	protected $username;

Then i try to run public function with variable ($un), which is the username from fetchUD "testuser".

public function fetchUD($un) {

and at the end i got, which gives me my result. I'm wondering how can i handle the data separately just like i tried to do on the second part. "$object->fetchUD("testuser")->email"

return $this;

This is what i get out (which continues to all DB info etc)

object(AccountInformation)#1 (13) { ["username":protected]=> string(8) "testuser" ["uid":protected]=> int(1) ["email":protected]=> string

--------------------

2nd thing, is there way to prevent var_dump from displaying the database information (which is private) from other class i made?

<?php
class ConnectDB {
	private $host;
	private $user;
	private $pass;
	private $db;
	
	protected function conDB() {
		$this->host = "";
		$this->user = "";
		$this->pass = "";
		$this->db = "";
		
		/* Makes the Database connection */
		$con = new mysqli($this->host,$this->user,$this->pass,$this->db);
		return $con;
	}
}
?>

Think i got how the system works now with private, protected and public. If im correct i can only use public variablesfor example

public $username

which then i can access via this 

$object->fetchUD("testuser")->username

 

Edited by Mudsaf
added db thing

Share this post


Link to post
Share on other sites

Yes, only public members can be access from outside the class.  It's common to make things private and then have public getter and setter methods:

class AccountInformation {

  private $fname;

  public function __construct($fname = '') {
    $this->fname = $fname; 
  }

  public function getFname() {
    return $this->fname;
  }

  public function setFname($fname) {
    $this->fname = $fname;
  }
}

$acct = new AccountInformation();
$acct->setFname('Steve');
echo $acct->getFname();

The difference between protected and private is only with extended classes.  Neither of them can be accessed from outside of your class.   A protected member can be accessed from a class that extends your class, but a private member can only be accessed from inside your class.

  • Like 1

Share this post


Link to post
Share on other sites

Got any tips for including files with OOP system? At the moment i can do like this.

include_once "inc/ps.db_con.php";
include_once "inc/script.myinformation.php";

But i assume i should do ps.db_con.php call inside script.myinformation.php instead, since it always requires it. (Like below)

include_once "inc/ps.db_con.php";
class MyInformation extends ConnectDB {

This works if i'm in index.php file, but if i want to go subfolder it stops working. I could use "/" path, but i've always seen it as inconvenient way. Example below.

include_once "/myproject/inc/ps.db_con.php";
class MyInformation extends ConnectDB {

 

 

Edited by Mudsaf

Share this post


Link to post
Share on other sites

If you want to include files that also include files, you need to use a relative to root path. Otherwise, the context of your files will change. index.php at /myproject/ works fine, because you have /myproject/inc/ps.db_con.php, but when you go into inc/script.myinformation.php the link would instead need to be ps.db_con.php because they're in the same directory.

Your relative to root path you've suggested above is incorrect, as the HTML style paths only works when you have a server to check the root against. Because this is the server processing, we have no such luxury.

My solution I use is as follows.

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . "/relativeToRoot/path/to/file.php";

In your case it would be

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . "/myproject/inc/ps.db_con.php";

 

  • Like 1

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...