variable not recognized PHP POO - php

I've a file data.php with different classes.
And in other file I make the require call.
<?php
include_once('data.php');
$db= new dataManager(); // here
class Manager{
//$db= new dataManager(); here doesn't work an error occurs
function savingData($ob )//obj
{
$db.saveData($ob);//error undefined variable $db
}

try with -> to call function with objects in php
$db->saveData($ob);
Also you need to pass $ob to manager class function
$newobj= new Manager();
$newobj->savingData($ob);

Okey after search I remembered global word
only we need to add
global $variable; in every function.
works yet.
<?php
include_once('data.php');
$db= new dataManager(); // here
class Manager{
function savingData($ob )//obj
{
global $db; //<---
$db.saveData($ob);//error solved
}

Related

public variable in php class not working

I have php class like
class muhasebe {
public $smh_id;
public function smh_kayitekle($data){
global $conn;
$stok_miktar_hareket="INSERT INTO `smh`( `urun`, `iscilik`, `birim`, `adet`, `miktar`)
VALUES
('$urun',
'$iscilik',
'$birim',
'$adet',
'$miktar')";
$conn ->exec($stok_miktar_hareket);
*****$this ->smh_id***** = $conn->lastInsertId();
}
}
when run code, part between **** not working.
$conn is a pdo variable my aim is to use smh_id in another function in the class. when I use $this->smh_id without $this prefix ($smh_id), it is okey
but then I can not use in other function.
If smh_id is a variable you have to write
$this->$smh_id = //code here

Calling PHP Class Function In My Page

I have a class file: we'll call it class.php. The functionality of that is to grab info from an ini file (yeah, I posted the question about security and was given the great suggestion to use either a config file or an ini file to hold the DB information).
Essentially, my class is this:
<?php
class myClass
{
public function getAttached()
{
$file = "../../myFile.ini";
if (!$settings = parse_ini_file($file, TRUE)) throw new exception('Unable to open ' . $file . '.');
$hoost = $settings['mysqli']['default_host'];
$useer = $settings['mysqli']['default_user'];
$pazz = $settings['mysqli']['default_pw'];
$dbs = $settings['mysqli']['default_db'];
$con = mysqli_connect($hoost ,$useer, $pazz, $dbs);
return $con;
}
}
$obj = new myClass();
$obj->getAttached();
$vals = $obj->getAttached();
//echo $vals; //didn't know if I should echo this or not.
?>
I want to call this in my somePage.php file to make my "mysqli" connection and go from there...
I tried this:
require_once('class.php');
getAttached();
Obviously that didn't work (I knew it wouldn't but - I did it anyway just to see if "maybe"), so - how do I call that function from my class file in the regular php page?
Any thoughts would be appreciated.
Thanks in advance.
You need to make an instance of the class before calling the functions as they're not static.
require_once('class.php');
$myClass = new myClass();
$myClass-> getAttached();
or, like I said above you could make the function static.
public static function myFunction() {
//etc...
}
Then to call it you would use:
require_once('class.php');
myClass::getAttached();
You have to instanciate your class first, the same way you did it in you class.php file:
$myclass = new myClass();
$myClass->getAttached();
Note that if your method can be used without any relation with your class, you could make it static:
public static function getAttached() {
// ...
}
And use it without having to instanciate your class:
myClass::getAttached();
Your getAttached() method within the myClass ,create the instance for the class and call
the function
$call = new myClass();
$call->getAttached();
Given answers are correct, but if you keep your class file as you posted, you have object already in $obj so there is no need to make new one. If it is just temporary you can ignore my post.
One more thing:
$obj->getAttached(); // this line is not needed, as you call this function in next line
$vals = $obj->getAttached();

Use classes of php before declaring

I wonder how i can call a method of a class inside of a method in other classes that called method is in a class that declared after the classes that use inside.
Let me show you an example:
Database.php:
class Database{
modifyUser($field,$change){
global $logging;
if($database->query('MYSQL QUERY')){
$logging->modifyUser($field,$change);
}
}
}
$database = new Database;
Logging.php:
class Logging{
modifyUser($field,$change){
global $database;
$database->query('Mysql Query for Logging');
}
}
$logging = new Logging;
now problem is if use top classes in included file like this:
user.php
include('database.php');
include('logging.php');
php shows an error that you use $logging variable which is not declared as class yet.
and if I change like this:
user.php
include('logging.php');
include('database.php');
same error but for $database; I don't know how to use it that PHP doesn't conflict for sequences.
I don't think it is possible, if you run the following code with command line you'll get "Segmentation fault" error;
class A
{
private $b;
public function __construct()
{
$b = new B();
}
}
class B
{
private $a;
public function __construct()
{
$a = new A();
}
}
$x = new A();
it is like an infinite loop.
First of all you can use include_once / require_once, so only the first time the file is needed, it will be loaded
Also, you can use autoloading
And instead of using global variables, either pass the instance or provide some sort of factory. Relying on global variables is bad for your encapsulation and it's ugly in an OO code

php access db class from inside a function

My database class works perfectly and I call it like this $db = new Database('user', 'pass', 'db', 'host');. I top of my script I am defining this database, but later in the script I am trying to use the connection inside a function, but it seems to that the $db is not global, so my function can't access it. I have the possibility to create a new database class connection inside every function in my script, but I really would like to access the $db as a global access point.
Here is some code:
require_once('database_class.php');
$db = new Database('user', 'pass', 'db', 'host');
// I can reach the $db here and make the $db->PDO->'statement'();
function userExists($user) {
$bool = false;
// But in here I can't access $db...
$query = $db->PDO->query('SELECT * FROM login WHERE username = "$user"');
$result = $query->fetch();
if ($result) {
// User exists
$bool = true;
}
return $bool;
}
Put global $db at the beginning of the function like so:
function userExists($user) {
global $db;
// Rest of code here
Variables within functions only exist locally by default in PHP. To use a variable declared outside of a function which is not passed as an argument (e.g $user) you need to use a global variable as shown above.
You could probably just modify your functions to take the $db var as an argument. e.g. :
function userExists($user, $db) {
...
}
Objects are passed by reference by default (see here) so you won't be inadvertently making copies of the $db object with each call.
I use classes for connections and queries too. But it would help that you define already in your class the variables needed for connection, so you don't have to repeat them in every page of code.
and maybe this? use the db as an argument.
function userExists($user, $db) { //codecodecode }

problem in variables scope in PHP

I wrote this code
require('Database.class.php');
function get_info (){
$db = new Database($config['server'], $config['user'], $config['pass'], $config['database'], $config['tablePrefix']);
$db->connect();
$sql = $db->query('SELECT * FROM ja_cat');
while ($options = $db->fetch_array($sql)) {
$cat[].=" ".$options['title'];
}
$db->close();
return $cat;
then I get this Mysql error
Mysql Error : No database selected .
but when I put the require instruction inside the function it's work fine
My guess is Database.class.php creates some variables that are probably global in scope that it relies upon. If you require it inside the function and it works, that supports that theory. Is that your class? Can you change it? Can you post it?
Basically $config needs a global qualifier inside the function.
Make this the first line IN your get_info() function:
global $config;
Also, you may want to define $db outside of the function at the start of the code and then close the connection at the end, instead of having to re-connect multiple times.
You have to import the global variable $config into the scope of the function:
function get_info() {
global $config;
}

Categories