How to send variable value into php class?

Asked

Viewed 55 times

2

I have been perfecting the techniques of safe use with database connection and arrived at the script below.

<?php class query_sql {
protected static $conect;
private function __construct() {

    $mysql_host = "";
    $mysql_base = "";
    $mysql_user = "";
    $mysql_pass = "";

    try { self::$conect = new PDO("mysql:host=" . $mysql_host . "; dbname=" . $mysql_base, $mysql_user, $mysql_pass);
        self::$conect -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        self::$conect -> exec("set names utf8");
        $conect = null; }

    catch(PDOException $error) {
        echo $error -> getMessage(); } }

public static function conect_sql() {
    if (!self::$conect) {
        new query_sql; }
    return self::$conect; } }

$bind_sql = query_sql::conect_sql(); ?>

my next step is to learn how to send

    $mysql_host = "";
    $mysql_base = "";
    $mysql_user = "";
    $mysql_pass = "";

together with the values below

<?php $list_information = $bind_sql -> prepare("SELECT user_email, user_name FROM usuarios WHERE user_email = :user_email LIMIT 1");
$list_information -> bindValue(":user_email", "@");
$list_information -> execute();
$list_information = $list_information -> fetchAll(); ?>

only list above and compare below (login as example)

<?php $compare_information = $bind_sql -> prepare("SELECT user_email, user_name FROM usuarios WHERE user_email = :user_email AND user_name = :user_name LIMIT 1");
$statement_array = array(":user_email" => "@", ":user_name" => "*");
foreach($statement_array as $array_key => $array_value) {
    $compare_information -> bindValue($array_key, $array_value); }
$compare_information -> execute();
$compare_information = $compare_information -> fetchAll(); ?>

To receive so

<?php foreach($list_information as $list_information) {
echo $list_information["user_email"] . " | " . $list_information["user_name"]; } ?>

and so

<?php foreach($compare_information as $compare_information) {
echo $compare_information["user_email"] . " | " . $compare_information["user_name"]; } ?>

I just need a little help as to what I can learn so that I can send the variables into the class so they don’t have to be in there, which is why I have a database for every piece of information, the destination of this script uses 4 databases so I would define which database to use in each prepare (select, delete, att etc.)

  • First you should understand about visibility. It is mixing with static methods and the constructor is as private. http://php.net/manual/en/language.oop5.visibility.php

1 answer

2


An alternative is to create an auxiliary (private) method that reads a file with the connection parameters and returns them to the constructor.

Let’s say the file has the values separated by ; (this is just an example! do not leave user and password exposed)

localhost;loja2015;usuario;senha 

php:

private function getConnectionArgs($file="caminho padrão"){
    return explode(';', file_get_contents($file));
}

Your constructor should add the new method call:

private function __construct() {
   $args = self::getConnectionArgs();
   self::$conect = new PDO("mysql:host=" . $args[0] . "; dbname=" . $args[1], $args[2], $args[3]);

You can make it a little more flexible if you pass the file on the call of conect_sql() that the first time will pass it to the builder.

  • 1

    Return exploe(';', file_get_contents($file)); I believe the letter 'e' is missing from the explode.. I tried editing, but it is not allowed when editing is less than 6 characters..

  • @Thiagobarros missed even the d xD thank you, corrected!

  • the method is really great, but wanted to avoid using too many files, wouldn’t it be like in place of a file, I use an array or a variable as follows? $Infor = "server;base;user;pass"; private Function getConnectionArgs($file = $Infor) { Return explode(';', $file); } .

  • as for flexibilizing in conect_sql() it made me very happy, could you give me an example? I’m learning a lot.

  • would have as I send the login information inside the $bind_sql = query_sql::conect_sql(""here""); because this would be very good, I would add one of these in each function I have, which are several and each of them uses the four different databases, ie i want to log the user there would be the login function and would have one of these with connection of the logins database then after authenticated would in the database with another of these

Browser other questions tagged

You are not signed in. Login or sign up in order to post.