I'm learning how to use classes properly... I'm looking at usercake and most of it makes sense, however I'm not sure what the __construct function is doing. I understand it gets called when you create the class... i.e. $loggedInUser = new loggedInUser();
What does the stuff below do and why do I need it?
function __construct($user, $display, $title, $pass, $email)
{
//Used for display only
$this->displayname = $display;
//Sanitize
$this->clean_email = sanitize($email);
$this->clean_password = trim($pass);
$this->username = sanitize($user);
$this->title = sanitize($title);
if(usernameExists($this->username))
{
$this->username_taken = true;
}
else if(displayNameExists($this->displayname))
{
$this->displayname_taken = true;
}
else if(emailExists($this->clean_email))
{
$this->email_taken = true;
}
else
{
//No problems have been found.
$this->status = true;
}
}
Edit: Here is how the class gets called:
$loggedInUser = new loggedInUser();
$loggedInUser->email = $userdetails["email"];
$loggedInUser->user_id = $userdetails["id"];
$loggedInUser->hash_pw = $userdetails["password"];
$loggedInUser->title = $userdetails["title"];
$loggedInUser->displayname = $userdetails["display_name"];
$loggedInUser->username = $userdetails["user_name"];
$loggedInUser->alerts = array();
It is the constructor function. When you create an instance of that class your constructor function is run.
For example, with your constructor (I don't know your class name).
$class = new MyClass("jdoe", "John Doe", "Standard User", "Passw0rd!","jdoe#example.com");`
This will create a new MyClass and store it in $class.
As for its purpose, it lets you initialize the object to a starting state of some kind. You can populate properties, set default values, or just do nothing. It is really application specific.
EDIT (in response to OP's edit)
I would really suggest keeping your object properties either protected or private and use setter/getters to access that data. You are giving public access to your objects properties, which isn't bad, but it can lead to accidentally changing something you didn't mean to change. Maybe you should consider something like this:
<?php
class LoggedInUser
{
private $id;
private $username;
private $password;
private $displayname;
private $email;
private $title;
public function __construct($id, $username, $password, $displayname, $email, $title)
{
$this->setID($id);
$this->setUsername($username);
$this->setPassword($password);
$this->setDisplayName($displayname);
$this->setEmail($email);
$this->title($title);
}
public function sanitize($var)
{
//Sanitize $var and then...
return $var;
}
public function setID($num)
{
$this->id = $this->sanitize($num);
}
public function setUsername($string)
{
$this->username = $this->sanitize($string);
}
//Keep adding other "set" methods.
}
?>
Then to use this you would do something like:
$loggedin = new LoggedInUser( "arg1", "arg2", "etc..." );
Now your object is setup with the starting state. If you need to change a property later you can always do:
$loggedin->setTitle("Correct Title");
Make sure you create functions to return your properties as well. In the example above your properties are private so a call to $loggedin->title would generate an error in PHP
// Set construct function which will run when your class is called
function __construct($user, $display, $title, $pass, $email)
{
// Sets display name
$this->displayname = $display;
// Sanitizing user inputted data (See SQL injection/XSS attacks)
$this->clean_email = sanitize($email);
$this->clean_password = trim($pass);
$this->username = sanitize($user);
$this->title = sanitize($title);
// Check if any duplicates of the user inputted data exist already in the database
// If any of these checks return true, the status wont be set to true, and further code wont be ran
if(usernameExists($this->username))
{
$this->username_taken = true;
}
else if(displayNameExists($this->displayname))
{
$this->displayname_taken = true;
}
else if(emailExists($this->clean_email))
{
$this->email_taken = true;
}
else
{
// No duplicate information has been found, set status and continue registration
$this->status = true;
}
}
You need it because initialize the object you create.
Related
I am using https://github.com/seaofclouds/tweet
I want to pass index.php?consumer_key=xxx.
After passing this I want to get in the PHP class but i am getting error.
Here is the Code below. Full Code: http://pastebin.com/gCzxrhu4
//$consumer_key = $_GET['consumer_key'];
class ezTweet {
private $consumer_key = $consumer_key; // getting error
private $consumer_key = 'xxxxx'; // working
private $consumer_secret = 'xxxx';
//other things
}
Please Help.
A class is a stand-alone, re-usable and portable unit of code.
Therefore, it should never, ever, under any circumstances, rely on GLOBAL variables to initialize its properties or do its job.
If you need a class to have access to a value, pass that value to the class, either through the constructor or a setter method:
class EzTweet
{//class names start with UpperCase, and the { goes on the next line
private $consumer_key = null;//initialize to default, null
public function __construct($cKey = null)
{//pass value here, but default to null -> optional
$this->consumer_key = $cKey;//set value
}
public function setConsumerKey($cKey)
{
$this->consumer_key = $cKey;//set value later on through setter
return $this;
}
}
//use:
$ezTwitter = new EzTwitter();
if (isset($_GET['consumer_key']))
$ezTwitter->SetConsumerKey($_GET['consumer_key']);//set value
That's what I'd do, anyways. BTW: please check, and try to stick to, the coding standards.
Update:
It turns out you already have a constructor. Fine, just change it a bit to:
public function __construct($cKey = null)//add argument
{
$this->consumer_key = $cKey;//add this
// Initialize paths and etc.
$this->pathify($this->cache_dir);
$this->pathify($this->lib);
$this->message = '';
// Set server-side debug params
if ($this->debug === true)
error_reporting(-1);
else
error_reporting(0);
}
You can't set the property class value with a $variable, in this case you need before construct your class.
class ezTweet
{
private $consumer_key = '';
private $consumer_secret = 'xxxx';
public function __construct($consumer_key)
{
if (!is_string($consumer_key) || !strlen(trim($consumer_key))) {
throw new \InvalidArgumentException('"consumer_key" cannot be empty!');
}
$this->consumer_key = $consumer_key;
}
public function getConsumerKey()
{
return $this->consumer_key;
}
}
$consumer_key = (isset($_GET['consumer_key']) ? $_GET['consumer_key'] : null);
try {
$ezTweet = new ezTweet($consumer_key);
// [...]
}
catch (\InvalidArgumentException $InvalidArgumentException) {
echo $InvalidArgumentException->getMessage();
}
We are trying to understand the best way to use mysqli/other classes in multiple custom classes so that we don't instantiate a new object every time.
Is the code below the best/correct way of doing this?
The functions are only examples.
Thank you :)
<?php
class Base {
public function __get($name) {
if($name == 'db'){
$db = new mysqli('**', '*s', '*', '*');
$this->db = $db;
return $db;
}
if($name == 'blowfish'){
$blowfish = new PasswordHash(8, true);
$this->blowfish = $blowfish;
return $blowfish;
}
}
}
class A extends Base {
public function validate($username, $password) {
$query = $this->db->query("SELECT * FROM users");
return $query->num_rows;
}
public function password($password)
{
return $this->blowfish->HashPassword($password);
}
}
class PasswordHash {
public function __construct($iteration_count_log2, $portable_hashes) { }
public function HashPassword($password) {
return $password;
}
}
$a = new A;
echo $a->validate('test','test'); // returns number rows count as expected
echo $a->password('password123'); // returns password123 as expected
?>
You are/should probably be more interested in Dependency Injection instead of creating a tight coupling of Base|A and the MySQL database.
I am currently using something like
class User{
/* #var Contacts*/
public $contacts = array();
}
$userObj = new User();
$userObj->contacts[] = new Contact(...);
$userObj->contacts[] = new Contact(...);
Tough we can document the type of variable using phpDocumentor, is it also possible to restrict other types of objects to be assigned to the contacts array
$userObj->contacts[] = 2.3 //should be considered as invalid
Not how it works in php
Here is what you can do instead
class User{
/* #var Contacts*/
private $contacts = array();
public function setContacts(Contact $contact){
$this->contacts[] = $contacts;
}
}
No you can use it like so
$userObj = new User();
$userObj->setContacts(new Contact(...));
And the following will cause an error
$userObj->setContacts(2.3);
Declare $contacts as private and use getter and setter methods.
Class User{
private $contacts = array();
function addContact($contact) {
if (is_object($contact) && get_class($contact) == "Contact") {
$this->contacts[] = $contact;
} else {
return false;
// or throw new Exception('Invalid Parameter');
}
}
function getContacts() {
return $this->contacts;
}
}
In this little example below in PHP what would be a good way to be able to create the variables in the user class and then be able to use them on any page where I create a user object?
<?PHP
//user.class.php file
class User
{
function __construct()
{
global $session;
if($session->get('auto_id') != ''){
//set user vars on every page load
$MY_userid = $session->get('auto_id'); //user id number
$MY_name = $session->get('disp_name');
$MY_pic_url = $session->get('pic_url');
$MY_gender = $session->get('gender');
$MY_user_role = $session->get('user_role');
$MY_lat = $session->get('lat');
$MY_long = $session->get('long');
$MY_firstvisit = $session->get('newregister');
}else{
return false;
}
}
}
?>
<?PHP
// index.php file
require_once $_SERVER['DOCUMENT_ROOT'].'/classes/user.class.php';
$user = new User();
//how should I go about making the variables set in the user class available on any page where I initiate the user class?
// I know I can't use
// echo $MY_pic_url;
// 1 way I can think of is to have the class return an array but is there another way?
?>
To elaborate on Lance' answer; if the point of the class is to be nothing more than an container for the data, in stead of doing something with the data you're pretty safe. But a good principal of OOP is to stick to encapsulation. Encapsulation means, amongst other things, that you hide the inner details of your object from the outside and only let the outside access the fields through it's interface methods.
Let's say you don't want the fields in the User object to be altered from the outside, but only accessed, then you'ld be better of with something like the following:
class User
{
private $_userId;
// and a bunch of other fields
public function __construct( $data )
{
// do something with the data
}
public function getUserId()
{
return $this->_userId;
}
// and a bunch of other getters to access the data
}
In all honesty, you could use magic methods like __set and __get to simulate what you want and catch any unwanted altering in the __set method.
Furthermore, I wouldn't use the session as a global variable. You should pass the session object as an argument to it's constructor (like I illustrated in the example). This enforces loose coupling. Because now your User objects are tied to the global session object, but with passing it to the constructor any data could be passed in. This makes the class more flexible.
Edit:
Here's an example of how you could pass an object (for instance your session object) to the constructor. One thing to keep in mind is that, the way your session object is designed, it still, somewhat, enforces tight coupling, because it mandates getting properties through the get() method.
class User
{
public function __construct( $data )
{
$this->_id = $data->get( 'id' );
$this->_firstname = $data->get( 'firstname' );
// etc
}
}
// usage
$session = new YourSessionObject();
$user = new User( $session );
You have a few options at hand to propagate loose coupling, and making you User object a little more flexible.
Mandate that the data for you User object is provided as:
distinct arguments
class User
{
protected $_id;
protected $_firstname;
// etc;
public function __construct( $id, $firstname, /* etc */ )
{
$this->_id = $id;
$this->_firstname = $firstname;
// etc
}
}
// usage
$session = new YourSessionObject();
$user = new User( $session->get( 'id' ), $session->get( 'firstname' ), /* etc */ );
array
class User
{
protected $_fields = array(
'id' => null,
'firstname' => null,
// etc
);
// dictate (type hint) that the argument should be an array
public function __construct( array $data )
{
foreach( $data as $field => $value )
{
if( array_key_exists( $field, $this->_fields )
{
$this->_fields[ $field ] = $value;
}
}
}
}
// usage
$session = new YourSessionObject();
$array = /* fill this array with your session data */;
$user = new User( $array );
implementing some interface
// objects that implement this interface need to implement the toArray() method
interface Arrayable
{
public function toArray();
}
class User
{
protected $_fields = array(
'id' => null,
'firstname' => null,
// etc
);
// dictate (type hint) that the argument should implement Arrayable interface
public function __construct( Arrayable $data )
{
// get the data by calling the toArray() method of the $data object
$data = $data->toArray();
foreach( $data as $field => $value )
{
if( array_key_exists( $field, $this->_fields )
{
$this->_fields[ $field ] = $value;
}
}
}
}
class YourSessionObject implements Arrayable
{
public function toArray()
{
/* this method should return an array of it's data */
}
}
// usage
$session = new YourSessionObject();
$user = new User( $session );
etc
There are a few other options, but this should give you some ideas. Hope this helps.
Make them public members:
class user
{
public $first_name;
function __construct()
{
$this->first_name = $_SESSION['first_name'];
}
}
$user = new user();
echo $user->first_name;
Sidenote: the constructor has no return value, i.e. return false does not have the effect you probably intended.
Either use public properties or protected properties+accessor methods.
Or store the $session in your object and then "delegate" each query for a property to that $session object.
class User
{
protected $session;
function __construct($session)
{
$this->session = $session;
}
function get($name) {
if( ''==$this->session->get('auto_id')) {
throw new Exception('...');
}
return $this->session->get($name);
}
}
$user = new User($session);
echo $user->get('disp_name');
Or use the "magic" __get() method, e.g.
class User
{
protected static $names = array(
'auto_id', 'disp_name', 'pic_url', 'gender',
'user_role', 'lat', 'long', 'newregister'
);
protected $properties = array();
function __construct()
{
global $session;
if($session->get('auto_id') != '') {
foreach(self::$names as $n) {
$this->properties[$n] = $session->get($n);
}
}
else {
throw new Exception('...');
}
}
function __get($name) {
return isset($this->properties[$name]) ? $this->properties[$name] : null;
}
}
$user = new User;
echo $user->disp_name;
Use attributes to store it.
<?PHP
//user.class.php file
class User
{
public $MY_userid;
public $MY_name;
public $MY_pic_url;
public $MY_gender;
public $MY_user_role;
public $MY_lat;
public $MY_long;
public $MY_firstvisit;
function __construct()
{
global $session;
if($session->get('auto_id') != ''){
//set user vars on every page load
$this->MY_userid = $session->get('auto_id'); //user id number
$this->MY_name = $session->get('disp_name');
$this->MY_pic_url = $session->get('pic_url');
$this->MY_gender = $session->get('gender');
$this->MY_user_role = $session->get('user_role');
$this->MY_lat = $session->get('lat');
$this->MY_long = $session->get('long');
$this->MY_firstvisit = $session->get('newregister');
}else{
return false;
}
}
}
?>
You can also save the user object in the $_SESSION variable after you have created it initially.
<?PHP
//user.class.php file
class User
{
function __construct()
{
var $MY_userid;
var $MY_name;
var $MY_pic_url;
var $MY_gender;
var $MY_user_role;
var $MY_lat;
var $MY_long;
var $MY_firstvisit;
global $session;
if($session->get('auto_id') != ''){
//set user vars on every page load
$this->MY_userid = $session->get('auto_id'); //user id number
$this->MY_name = $session->get('disp_name');
$this->MY_pic_url = $session->get('pic_url');
$this->MY_gender = $session->get('gender');
$this->MY_user_role = $session->get('user_role');
$this->MY_lat = $session->get('lat');
$this->MY_long = $session->get('long');
$this->MY_firstvisit = $session->get('newregister');
}else{
return false;
}
}
}
?>
<?PHP
// index.php file
require_once $_SERVER['DOCUMENT_ROOT'].'/classes/user.class.php';
$user = new User();
print $user->MY_name;
?>
I am a learner, I have a class db to help me connect and fetch results in mySQL.
$set = $db->get_row("SELECT * FROM users");
echo $set->name;
this way i use echo results outside a class.
Now i have created another class name user and it has this function
public function name() {
global $db;
$set = $db->get_row("SELECT * FROM users");
$this->name = $set->name;
}
after initializing the class user, when i try to echo $user->name i dont get expected results.
Note i have declared above var $name; in class user
I'm pretty concerned by several things I see here
The method name name() is terribly uncommunicative as to what the method is supposed to do. Remember, methods are actions - try to give them some sort of verb in their name.
Usage of global in a class (or even usage of global period) when you should be using aggregation or composition.
You don't show any execution examples, but I can only assume you never actually call User::name(), which is why your test is failing
Here's some code that addresses these concerns.
<?php
class DB
{
/* Your methods and stuff here */
}
class User
{
protected $db;
protected $name;
public function __construct( DB $db )
{
$this->db = $db;
}
public function getName()
{
if ( is_null( $this->name ) )
{
$set = $this->db->get_row( "SELECT * FROM users" );
$this->name = $set->name;
}
return $this->name;
}
}
$db = new DB();
$user = new User( $db );
echo $user->getName();
class DB
{
public function get_row($q)
{
# do query and store in object
return $object;
}
}
class User
{
public $name;
public function __construct()
{
$this->name();
}
public function name() {
global $db;
$set = $db->get_row("SELECT * FROM users");
echo "<pre>".print_r($set)."</pre>"; # make sure $set is returning what you expected.
$this->name = $set->name;
}
}
$db = new DB();
$user = new User();
echo $user->name;
I am very much sorry, i figured out that problem was on my part, i was using cookies and had two cookies set which were giving problems :(