473,379 Members | 1,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

How should I do this constructor...

Samishii23
246 100+
So I have a user login system. The login works.
What I want to do is make a variable array in the class available for general usage. Obviously in most cases set the data in the constructor.

My login / user class has the login stuff within it, as well as the user functions. In this class I have {bool LoggedIn()} that I use for login status verification. So the class could very well be constructed and the user isn't logged in.

So I make the constructor a login verification? Or keep the method? Or both? Use the method within the constructor ( if thats possible ) to login verify then set the details?

Not sure which is the best, and most efficient way.
Dec 4 '10 #1
12 1730
Dormilich
8,658 Expert Mod 8TB
could you elaborate on your problem, because I didn’t understand it (specifically, I didn’t get what the array has to do with the Login class).
Dec 4 '10 #2
Samishii23
246 100+
Sorry Dorm...

I have a class "Users". With a Method "bool LoggedIn()".
The class has the login functionality in it, and I was going to add to it an array variable with the user data.

My problem is, is the way I have it setup now, is that the method LoggedIn() is the determining way of telling wether or not the user is indeed logged in.

I'm just curious if it'll be easier to rework the constructor to do the verification or use LoggedIn() in the constructor to set the user data, or have two seperate login verifications. I say two, because each user access page will see LoggedIn() to allow access or not.

Really I'm just wondering which is the more effective way. Or something else that I'm not seeing?
Dec 4 '10 #3
Dormilich
8,658 Expert Mod 8TB
for your user class it is sensible, to store the user data. the obvious point to pass this data is through the constructor, although you could also use setter methods.

depending on how loggedIn() currently works, I don’t see the need of changing the method. and as such you can call this method whereever you want. if you need an opinion about the class’ code, feel free to post it (or send me a PM).
Dec 4 '10 #4
Samishii23
246 100+
I'll post it.
Feel free to give me any points or hints about it. I'm welcome to anything.

Thanks for the replies! Really appreciate it.
Attached Files
File Type: txt class-user.php.txt (13.1 KB, 572 views)
Dec 6 '10 #5
Dormilich
8,658 Expert Mod 8TB
Feel free to give me any points or hints about it.
it is good coding practice to make functions and methods lowercase and class names uppercase, see Userland Naming Guide

EDIT: do you have some kind of class synopsis or usage example?
Dec 6 '10 #6
Samishii23
246 100+
I just figured seeing LoggedIn() was easier then looking at logged_in() or loggedin.
Maybe that was from my short dive into C#.

This is my main page that requires the login.
Expand|Select|Wrap|Line Numbers
  1. session_start();
  2. require_once 'src/library.php';
  3. $Conn = DBConn();
  4. $User = new Users();
  5. $Program = new DM();
  6.  
  7. $UserLoggedIn = $User->LoggedIn();
  8. if ( $UserLoggedIn == false ) header('Location: login.php');
This is the only way I've thought of so far to do this.
Dec 6 '10 #7
Dormilich
8,658 Expert Mod 8TB
DBConn is your main database handler?
Dec 7 '10 #8
Samishii23
246 100+
Yes sir.
Expand|Select|Wrap|Line Numbers
  1. function DBConn() {
  2.     // FUTURE: Implement defined ERRORs
  3.     $Conn = @mysql_connect(CONFIG_DBHOST, CONFIG_DBUSER, CONFIG_DBPASS);
  4.     if ( !$Conn )
  5.         return false;
  6.     else {
  7.         $db = mysql_select_db(CONFIG_DBNAME, $Conn);
  8.         if ( !$db )
  9.             return false;
  10.         else
  11.             return $Conn;
  12.         }
  13.     }
Dec 7 '10 #9
Dormilich
8,658 Expert Mod 8TB
Ok, I looked a bit more at the class and I can talk a bit about my thoughts.

Firstly, this class design is PHP 4. I would take the time to code it into PHP 5 (i.e. __construct() instead of User()).

Another point that makes the class difficult to understand is the $args parameter. though it may be convenient, I (personally) have no idea what keys are allowed and which values are allowed. I’d rather make each key an own parameter with its own default value (this also spares you the work of calling the array elements each time). You also should look into Type-Hinting.

I also see a lot of mysql_* functions and SQL Query string creation. for the former, look into PDO or MySQLi (the mysql_* functions are outdated!), for the latter, consider a separate DB handler (that may be a set of functions or a class). DB handling code does not belong in the User class, you may define a parent class that handles it. additionally, there is no prevention of SQL Injection attacks (easiest solved by Prepared Statements, for which you require PDO or MySQLi).

One important thing is error handling. currently your functions/method return different data types, which require you to always check the return value. check out Exceptions, I think your code will greatly benefit from it*.



* – PDO can use Exceptions to handle errors, this really cleans your code up!
Dec 7 '10 #10
Samishii23
246 100+
Thanks for the response!
I'd like to point out really quick that I don't have a constructor currently. I have a __construct() in there thats commented out, which is why I had made this thread.
Also the class is named "Users" while I have the "User" method in there, its not the constructor.
Also, I use the $args array because I'm trying to make the methods a little more dynamic if possible. So some of the $args could be different, if the arg in the method I need do I do something like this:
Expand|Select|Wrap|Line Numbers
  1. method(null, null, null, "data");
Just quickly. Is PDO an off branch of MySQL or something like that? Cause my web host only offers MySQL(i).

Other then that thanks, i'll look into what you mentioned.
Dec 7 '10 #11
Dormilich
8,658 Expert Mod 8TB
I'd like to point out really quick that I don't have a constructor currently.
you have. it is called User() (that‘s the way PHP 4 defined the class constructor, which is still working in PHP 5 (for compatibility reasons) unless there is a __construct() method defined)

Just quickly. Is PDO an off branch of MySQL or something like that? Cause my web host only offers MySQL(i).
PDO is PHP’s native Database Abstraction Layer and as such part of the PHP default configuration. it works with most databases (as long as there is a database driver installed) and it is not related to MySQLi (only by intend).

for me this would be reason enough to change the web hoster. but maybe they’ll install if if you ask nicely.

Also, I use the $args array because I'm trying to make the methods a little more dynamic if possible. So some of the $args could be different, if the arg in the method I need do I do something like this:
depending on which args you pass least often, default values make the calls easier. e.g.

Expand|Select|Wrap|Line Numbers
  1. function hash($data, $algo = "sha256", $salt = true, $length = 128)
  2. {
  3.     // function body
  4. }
  5.  
  6. // a very simple hash call:
  7. // if not explicitly overwritten, it uses the default values
  8. hash("the string to hash");
  9. // with a different algorithm
  10. hash("the powers to be", "ripemd160");
  11. // default w/o salt
  12. // note that you can only leave out the last values
  13. hash("what ever should be hashed", "sha256", false);
Dec 7 '10 #12
Samishii23
246 100+
I rewrote my class hash method a little bit.
Expand|Select|Wrap|Line Numbers
  1. // Hashing function
  2. // Requires $type and $str
  3. private function Hash($type, $str, $len=128) {
  4.     if ( $type == '' || $str == '' )
  5.         return false;
  6.  
  7.     // Do Password Hash
  8.     switch($type) {
  9.         case 'password': // Login passwords
  10.             // If Salt needs to be used
  11.             if ( $this->UseSalt )
  12.                 $str = $this->HashSalt . $str;
  13.             $str = hash($this->HashChoice, $str);
  14.             break;
  15.         case 'login-key': // Random hash for login keys
  16.             $str =  hash($this->HashChoice, mt_rand(100000,1000000));
  17.             break;
  18.         }
  19.  
  20.     // If theres a different length specified
  21.     if ( $len != 128 )
  22.         $str = substr($str, 0, $len);
  23.  
  24.     // Return the hash
  25.     return $str;
  26.     }
A little better by your standards?
I didn't include the hash type or salt because its specified in the class "Options".
Expand|Select|Wrap|Line Numbers
  1. private $HashChoice = 'sha512';
  2. private $UseSalt = false;
  3. private $HashSalt = '(19|1|&[(8g5NcY`QvOr]<p$Or'; 
Dec 7 '10 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Manoj | last post by:
Hi , I have written a simple program usinga class. But it not compiling.The error i get is my = myclass("Paul", "John") TypeError: this constructor takes no arguments I checked some previous...
3
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
0
by: John Haigh | last post by:
Should possibly have an "add" method in a PostingObject class in the constructor of PostingObject. will call to add a Posting to the ? Does this make sense? My sense is that a PostingObject needs a...
4
by: murat oguzalp | last post by:
i want to call base and this constructor at the same time. is is possible? i mean: B(int a, int b, int c):base(a):this(b) { // do something with c } at java i used to do that:
6
by: Henry | last post by:
I was trying to derive a class from System.Windows.Forms.ComboBox. My goal was to create a class that loaded its own data. I did not want to create too many objects, so I tried to share a Database...
4
by: pat | last post by:
Hi, I have been asked for an exam question to implement the constructor, copy constructor and destructor for the following class that describes an n-by-n matrix containing n squared integer...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
2
by: DaTurk | last post by:
Hi, I was wondering if there is a CLI equivalent to using the this keyword to overload constructors. You know where you would do something like MyClass() : this("something") { }
1
by: python101 | last post by:
class double: def __ini__(self,x1,y2): self._x=x1 self._y=y2 def result(self): return (self._x)*(self._y) def...
2
by: aki | last post by:
#include<iostream> using namespace std; class string { char *str; public: string(char *st) { str=st;
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.