Mudsaf Posted June 4, 2019 Share Posted June 4, 2019 (edited) 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 June 4, 2019 by Mudsaf Link to comment Share on other sites More sharing options...
justsomeguy Posted June 4, 2019 Share Posted June 4, 2019 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. 1 Link to comment Share on other sites More sharing options...
Mudsaf Posted June 4, 2019 Author Share Posted June 4, 2019 Thanks for the help, very much appreciated! Yes i have lived here all my life 😃 Link to comment Share on other sites More sharing options...
Mudsaf Posted June 4, 2019 Author Share Posted June 4, 2019 (edited) 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 June 4, 2019 by Mudsaf added db thing Link to comment Share on other sites More sharing options...
justsomeguy Posted June 4, 2019 Share Posted June 4, 2019 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. 1 Link to comment Share on other sites More sharing options...
Mudsaf Posted June 5, 2019 Author Share Posted June 5, 2019 (edited) 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 June 5, 2019 by Mudsaf Link to comment Share on other sites More sharing options...
Funce Posted June 5, 2019 Share Posted June 5, 2019 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"; 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now