When I try to execute this code from inherited class I got this error Using $this when not in object context
here my code
abstract class Connection {
private static $host;
private static $username;
private static $password;
private static $database;
public function __construct() {
self::$host = 'localhost'; // database server address
self::$username = 'root'; //database server username;
self::$password = ''; //database server password;
self::$database = 'oms'; //database name
}
private function connect() {
return new PDO('mysql:host=' . Connection::$host . ';dbname=' . Connection::$database . '', Connection::$username, Connection::$password);
}
protected function execute($sql) {
$this->connect();
return $this->connect()->query($sql);
}
}
what are the reason for that? i don't use any static method in Connection class. SoWhy do give this error?
This is probably what you want:
abstract class Connection {
protected static $host;
protected static $username;
protected static $password;
protected static $database;
protected static $connection;
public static function load() {
self::$host = 'localhost'; // database server address
self::$username = 'root'; //database server username;
self::$password = ''; //database server password;
self::$database = 'oms'; //database name
}
protected static function connect() {
// Only create the connection once
if (!self::$connection) {
self::$connection = new PDO('mysql:host=' . self::$host . ';dbname=' . self::$database . '', self::$username, self::$password);
}
return self::$connection;
}
public static function execute($sql) {
return self::$connect()->query($sql);
}
}
// Because we can't use an abstract class
class ConcreteConnection extends Connection {}
// Execute a SQL call
ConcreteConnection::execute("SELECT * FROM `table`");
But then what you'll have is a Singleton Pattern which is a pain for testing and changing later. I'd recommend you make it like this:
abstract class Connection {
protected $host;
protected $username;
protected $password;
protected $database;
protected $connection;
public function _construct($host, $username, $password, $database) {
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->database = $database;
}
protected function connect() {
if (!$this->connection) {
$this->connection = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->database . '', $this->username, $this->password);
}
return $this->connection;
}
public function execute($sql) {
return $this->connect()->query($sql);
}
}
// Because we can't use an abstract class
class ConcreteConnection extends Connection {}
// Inject our parameters into our class
$connection = new ConcreteConnection('host', 'username', 'password', 'database');
// Execute a SQL call
$connection->execute("SELECT * FROM `table`");
Related
I use laravel notification class for sending emails. Normaly for retring failed jobs in Job Class I can use $this->release() method. Do you have any idea how I can use this method in my notification class as well?
class OrderConfirmationNotification extends Notification implements ShouldQueue
{
use Queueable;
public $pdfAttachment;
public $determineSenderEmail;
public $mailData;
public $tries = 3;
public function __construct(array $mailData)
{
$this->mailData = $mailData;
$this->determineSenderEmail = $this->mailData['sender_email'];
}
/** #return string[] */
public function via(): array
{
return ['mail'];
}
/** #return MailMessage */
public function toMail(): MailMessage
{
return (new MailMessage)
->markdown($this->mailData['mail_template'], ['logoImage' => $this->mailData['company_logo']])
->from($this->determineSenderEmail['email'], $this->determineSenderEmail['name'])
->replyTo($this->determineSenderEmail['reply_to'], $this->determineSenderEmail['reply_name'])
->bcc($this->mailData['bcc_email']);
}
public function failed(Exception $exception)
{
Log::channel('xml_parse')->warning(sprintf('Order Confirmation notification failed'));
}
}
I want to write a javaagent using byte-buddy. I wan to intercept constructor of any class that is a sub class of java.sql.Connection.
my setup code:
log.info("{} profile activated", profile);
if (profile.equals("hikari")) {
startMatcher = nameStartsWith("com.zaxxer.hikari");
JDBCAPIInterceptor.profile = new HikariProfile();
} else if (profile.equals("dbcp1")) {
startMatcher = nameStartsWith("org.apache.commons.dbcp");
JDBCAPIInterceptor.profile = new DBCP1Profile();
} else if (profile.equals("dbcp2")) {
startMatcher = nameStartsWith("org.apache.commons.dbcp2");
JDBCAPIInterceptor.profile = new DBCP2Profile();
} else if (profile.equals("druid")) {
startMatcher = nameStartsWith("com.alibaba.druid.pool");
} else {
startMatcher = any();
}
new AgentBuilder.Default().type(startMatcher.and(isSubTypeOf(java.sql.Connection.class).or(isSubTypeOf(java.sql.Statement.class))))
.transform(constructorTransformer).transform(methodsTransformer).with(listener).installOn(inst);
but why constructor of org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper can't be intercepted while it's definitely a sub class of java.sql.Connection as it extends org.apache.commons.dbcp2.DelegatingConnection which implements java.sql.Connection?
also, my intercepting code is executed while constructor of org.apache.commons.dbcp2.DelegatingConnection being called.
public class PoolingDataSource<C extends Connection> implements DataSource, AutoCloseable {
/**
* PoolGuardConnectionWrapper is a Connection wrapper that makes sure a closed connection cannot be used anymore.
*
* #since 2.0
*/
private class PoolGuardConnectionWrapper<D extends Connection> extends DelegatingConnection<D> {
PoolGuardConnectionWrapper(final D delegate) {
super(delegate);
}
#Override
public void close() throws SQLException {
if (getDelegateInternal() != null) {
super.close();
super.setDelegate(null);
}
}
/**
* #see org.apache.commons.dbcp2.DelegatingConnection#getDelegate()
*/
#Override
public D getDelegate() {
return isAccessToUnderlyingConnectionAllowed() ? super.getDelegate() : null;
}
/**
* #see org.apache.commons.dbcp2.DelegatingConnection#getInnermostDelegate()
*/
#Override
public Connection getInnermostDelegate() {
return isAccessToUnderlyingConnectionAllowed() ? super.getInnermostDelegate() : null;
}
#Override
public boolean isClosed() throws SQLException {
return getDelegateInternal() == null || super.isClosed();
}
}
}
I have this class to connect to my database, but when I try to use it, always return null.
if I use these credentials in a single php file without class and static function, it works.
This is how I use it
<?php
include 'bdconexion.php';
$var ="hola";
$pdo=BaseDatos::Conectar();
print '<h3>PDO: simple select</h3>';
foreach($pdo->query( 'SELECT * FROM productos;' ) as $row)
{
echo $row['descripcion'];
}
Some help will be great
class BaseDatos
{
private static $bdname = 'bd';
private static $bdhost = 'localhost';
private static $user = 'root';
private static $pass = 'pass';
//private static $mssqldriver = '{SQL Server}';
//private static $mssqldriver = '{SQL Server Native Client 11.0}';
//private static $mssqldriver = '{ODBC Driver 11 for SQL Server}';
private static $conn = null;
public function __construct()
{
die('La funcion de inicio no es permitida');
}
public static function conectar()
{
if (null==self::$conn) {
try {
$conn = new PDO("mysql:host=".self::$bdhost.";dbname=".self::$bdname,self::$user,self::$pass);
} catch (PDOException $e) {
die($e->getMessage());
}
}
return self::$conn;
}
public static function desconectar()
{
self::$conn=null;
}
}
I want the user authentication in Yii to be based on user table in my database. This is my User model:
<?php
namespace app\models;
use Yii;
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\helpers\Security;
use yii\web\IdentityInterface;
/**
* This is the model class for table "user".
*
* #property integer $id
* #property string $username
* #property string $password
* #property string $title
*/
class User extends \yii\db\ActiveRecord implements IdentityInterface
{
public static function tableName()
{
return 'user';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['username', 'password'], 'required'],
[['username', 'password'], 'string', 'max' => 100]
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'UserID',
'username' => 'Username',
'password' => 'Password',
];
}
public static function findIdentity($id) {
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null) {
return static::findOne(['access_token' => $token]);
}
public static function findByUsername ($username){
return static::findOne(['username' => $username]);
}
public static function findbyPasswordResetToken($token)
{
$expire= \Yii::$app->params['user.passwordResetTokenExpire'];
$parts = explode('_', $token);
$timestamp = (int) end($parts);
if ($timestamp + $expire < time()) {
//token expired
return null;
}
return static::findOne(['password_reset_token' => $token ]);
}
public function getId() {
return $this->getPrimaryKey();
}
public function getAuthKey() {
return $this->auth_key;
}
public function validateAuthKey($authKey) {
return $this->getAuthKey() === $authKey;
}
public function validatePassword($password){
$this->password_hash= Yii::$app->security->generatePasswordHash ($password);
}
public function generateAuthKey()
{
$this->auth_key = Yii::$app->security->generateRandomKey();
}
/**
* Generates new password reset token
*/
public function generatePasswordResetToken()
{
$this->password_reset_token = Yii::$app->security->generateRandomKey() . '_' . time();
}
/**
* Removes password reset token
*/
public function removePasswordResetToken()
{
$this->password_reset_token = null;
}
}
But it is giving me this error when I try to login:
Setting unknown property: app\models\User::password_hash
This is actionLogin in siteController:
public function actionLogin()
{
if (!\Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
} else {
return $this->render('login', [
'model' => $model,
]);
}
}
And this is the code in LoginForm.php:
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
}
}
}
/**
* Logs in a user using the provided username and password.
* #return boolean whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
} else {
return false;
}
}
I don't know what is wrong, could you please help me fix this?
This is because the column "password_hash" assigned in the function "validatePassword()" doesn't exist in database or is not declared in the User model.
If the password hash is stored in the database in "password" field, then change "validatePassword()" to,
public function validatePassword($password)
{
return $this->password === Yii::$app->security->generatePasswordHash ($password);
}
Here is the solution
1: Declare it in the user controller
* #property string $password_hash
class User extends ActiveRecord implements IdentityInterface
{
public $password;
/**
* #inheritdoc
*/
public static function tableName()
{
return '{{%user}}';
}
public function rules()
{
return [
[['username','email','first_name','last_name','address','password','contact_no','role'], 'safe'],
[['email'], 'unique'],
[['email'], 'email'],
];
}
.
.
....
public function validatePassword($password)
{
return Yii::$app->security->validatePassword($password, $this->password_hash);
}
2: In your LoginForm model rule function add this
['password', 'validatePassword'],
everything else looks okay. Hope this helps you
This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 10 years ago.
I'm trying to make oop class to get user info but i get this error
Fatal error: Call to a member function prepare() on a non-object in functions.php
user.php - >
include_once 'functions.php';
$user = new User();
echo $user->get_fullname(5);
functions.php ->
include_once 'database.php';
class User
{
public function connect()
{
$dbh = new DB_Class();
}
public function get_fullname($uid)
{
$getName = $dbh->prepare("SELECT EmailAddress FROM users WHERE UserID =:username");
$getName->bindParam(':username', $uid);
$getName->execute();
$rowName = $getName->fetch();
$email = $rowName['emailaddress'];
return $email;
}
}
database.php - >
class DB_Class
{
public function connect() {
try {
$dbh= new PDO("mysql:host=localhost;dbname=root",'users','password', array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
));
} catch (PDOException $e) {
echo $e->getMessage();
}
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
$dbh->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
}
}
What i'm doing wrong :(
You never actually give access to the PDO instance $dbh to the things youre trying to use it in. It seems like youre using classes simple as groupings of functions but also expecting some magic to happen :-) This is how i would do it with your existingcode:
class DB_Class {
protected $dsn;
protected $user;
protected $password;
protected $connection;
public function __construct($dsn = null, $user = null, $password = null)
{
$this->dsn = $dsn;
$this->user = $user;
$this->password = $password;
}
public function connect()
{
if($this->connection) {
return $this->connection;
}
try {
$this->connection = new PDO($this->dsn,$this->user, $this->password,
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
));
$this->connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
$this->connection->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
return $this->connection;
}
catch (PDOException $e) {
return false;
}
}
}
class User {
protected $db;
public function __construct(DB_Class $db = null)
{
$this->db = $db;
}
public function setDb(DB_Class $db)
{
$this->db = $db;
}
public function get_fullname($uid)
{
$stmt = $this->db->connect()->prepare("SELECT EmailAddress FROM users WHERE UserID =:username");
$stmt->execute(array(':username', $uid));
if($row = $getName->fetch()) {
return $row['emailaddress'];
} else {
return null;
}
}
}
The $dbh variable isn't being passed into the get_fullname function. You'd need to do something like:
public function connect()
{
$dbh = new DB_Class();
return $dbh;
}
public function get_fullname($uid)
{
$dbh = $this->connect();
you need to declare $dbh as property of User class and access it via $this identifier:
class User
{
protected $dbh;
public function connect()
{
$this->dbh = new DB_Class();
}
public function get_fullname($uid)
{
$getName =$this->dbh->prepare("SELECT EmailAddress FROM users WHERE UserID =:username");
$getName->bindParam(':username', $uid);
$getName->execute();
$rowName = $getName->fetch();
$email = $rowMail['emailaddress'];
return $email;
}
}
You are never actually assigning your class properties as a property, all you are doing when declaring the variables inside of your methods is putting them into the methods current scope and they are destroyed once the method finishes execution.
To properly set a property within a class you need to declare them using $this->varname and the same goes for accessing them.
You are also using an incorrect name for the classes construction, the constructor must be set to __construct not connect.
As you can see in the updated code the $dbh variable is now a property of the User class and can be called between it's methods.
User Class
class User
{
public function __construct()
{
$this->dbh = new DB_Class();
}
public function get_fullname($uid)
{
$getName = $this->dbh->getConnection()->prepare("SELECT EmailAddress FROM users WHERE UserID =:username");
$getName->bindParam(':username', $uid);
$getName->execute();
$rowName = $getName->fetch();
$email = $rowName['emailaddress'];
return $email;
}
}
Database Class
class DB_Class
{
public function __construct() {
try {
$this->dbh = new PDO("mysql:host=localhost;dbname=root",'users','password', array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
));
} catch (PDOException $e) {
echo $e->getMessage();
}
$this->dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
$this->dbh->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
}
public function getConnection() {
return $this->dbh;
}
}
You will benefit greatly by using http://us2.php.net/manual/en/oop5.intro.php as a resource.