including class within another class in php - php

i have a php page which has some variables regarding database i.e server address, username and password etc.
config.php will include
<?php
$dbserver="";
$username="";
$password="";
$database="";
?>
i have a class which contains all the functions required for my website. How can i import my php page variables into this class to be used for the database connectivity?
my class
<?php
class a{
include("config.php");
function db_connect(){
mysql_connect($dbserver,$username,$password);
}
}
?>

usually for this purpose, Constants exist.
But if you want to use variables, all you have to do is to require_once(yourFile), then when you want to use these variables (which are global) inside a method of a class, simply refer to them as global $myVar; (as if you're declaring it). Only need to do this once for each global variable you want to use in the context.
Example:
settings.php:
$conn = 'my connection';
MyClass.php:
class MyClass
{
function DoSomething()
{
require_once('settings.php');
global $conn;
doSomethingWith($conn);
}
}

Update
For a Database class that requires configuration options, the simplest way would be to use the config values as parameters (example 1 of my original answer below).
A more complex, though also more flexible approach would be a Config-Class.
class Config
{
private $config = array();
public function __construct(array $config)
{
$this->config = $config;
}
public function Register($key, $value)
{
$this->config[$key] = $value;
}
public function Get($key)
{
if (!isset($this->config[$key])) return null;
return $this->config[$key];
}
}
Your DB class would look something like this:
class Database
{
private $config = null;
public function __construct(Config $config)
{
$this->config = $config;
}
public function Connect()
{
do_connect_stuff($this->config->Get('host'), $this->config->Get('user'), .....);
}
}
File config.php
<?php
$config = new Config(
array(
"host" => "localhost",
"user" => "user",
...
)
);
/*
alternative:
$config = new Config();
$config->Register('host', 'localhost');
$config->Register('user', 'user');
...
*/
?>
File that requires the database:
<?php
$database = new Database($config);
$database->Connect();
?>
As a side hint: Use PDO, it's far better than the old mysql_* functions.
Original Answer
The proper style would be to pass the variables to the functions as parameter or pass them when creating the object. You can also use Init methods to pass the parameters.
Examples:
(Which of the following code you should use depends on what you already have and how your code is designed, the 'cleanest' way would be an object for which you transmit the variables when calling the ProcessAction method)
Assuming that in your script you have a Variable $action which you get from $_GET or some other way.
Using an Object
class Controller
{
public function ProcessAction($action, $some_other_parameter, $and_yet_another_parameter)
{
[...]
}
}
You then call it with
$action = do_some_stuff_to_get_action();
$controller = new Controller();
$controller->ProcessAction($action, $other_parameter, $second_parameter);
Using a static class
class Controller
{
public static function ProcessAction($action, $some_other_parameter, $and_yet_another_parameter)
{
[...]
}
}
Called with:
$action = do_some_stuff_to_get_action();
Controller::ProcessAction($action, $other_parameter, $second_parameter);
Passing the parameters before calling the function
Object
class Controller
{
private $action = "";
private $some_other_parameter = "";
public function __construct($action, $some_other_parameter)
{
$this->action = $action;
$this->some_other_parameter = $some_other_parameter;
}
public function ProcessAction()
{
if ($this->action == 'do_stuff')
{
[...]
}
}
}
Called with:
$action = do_some_stuff_to_get_action();
$controller = new Controller($action, $other_parameter);
$controller->ProcessAction();
Static methods
class Controller
{
private static $action = "";
private static $some_other_parameter = "";
public static function Init($action, $some_other_parameter)
{
self::$action = $action;
self::$some_other_parameter = $some_other_parameter;
}
public static function ProcessAction()
{
if (self::$action == 'do_stuff')
{
[...]
}
}
}
Called with:
$action = do_some_stuff_to_get_action();
Controller::Init($action, $other_parameter);
Controller::ProcessAction();

I used the database configuration in the constructor of the class. I think that was the only solution not including any third page in the scenario.

Related

How to access global variable from inside class's function

I have file init.php:
<?php
require_once 'config.php';
init::load();
?>
with config.php:
<?php
$config = array('db'=>'abc','host'=>'xxx.xxx.xxx.xxxx',);
?>
A class with name something.php:
<?php
class something{
public function __contruct(){}
public function doIt(){
global $config;
var_dump($config); // NULL
}
}
?>
Why is it null?
In php.net, they told me that I can access but in reality is not .
I tried but have no idea.
I am using php 5.5.9.
The variable $config in config.php is not global.
To make it a global variable, which i do NOT suggest you have to write the magic word global in front of it.
I would suggest you to read superglobal variables.
And a little bit of variable scopes.
What I would suggest is to make a class which handles you this.
That should look something like
class Config
{
static $config = array ('something' => 1);
static function get($name, $default = null)
{
if (isset (self::$config[$name])) {
return self::$config[$name];
} else {
return $default;
}
}
}
Config::get('something'); // returns 1;
Use Singleton Pattern like this
<?php
class Configs {
protected static $_instance;
private $configs =[];
private function __construct() {
}
public static function getInstance() {
if (self::$_instance === null) {
self::$_instance = new self;
}
return self::$_instance;
}
private function __clone() {
}
private function __wakeup() {
}
public function setConfigs($configs){
$this->configs = $configs;
}
public function getConfigs(){
return $this->configs;
}
}
Configs::getInstance()->setConfigs(['db'=>'abc','host'=>'xxx.xxx.xxx.xxxx']);
class Something{
public function __contruct(){}
public function doIt(){
return Configs::getInstance()->getConfigs();
}
}
var_dump((new Something)->doIt());
Include the file like this:
include("config.php");
class something{ ..
and print the array as var_dump($config); no need of global.
Change your class a bit to pass a variable on the constructor.
<?php
class something{
private $config;
public function __contruct($config){
$this->config = $config;
}
public function doIt(){
var_dump($this->config); // NULL
}
}
?>
Then, if you
include config.php
include yourClassFile.php
and do,
<?php
$my_class = new something($config);
$my_class->doIt();
?>
It should work.
Note: It is always good not to use Globals (in a place where we could avoid them)

Using global variable in my class causing unexpected syntax

I have a class that requires a variable that is defined out of the scope of it. So i tried using global but, this causes this error:
syntax error, unexpected 'global' (T_GLOBAL), expecting function (T_FUNCTION)
I am unsure if I have put it in the wrong place or using the global keyword incorrectly.
My code looks like this:
$data = new testClass();
class System
{
private $values;
global $data;
public function __construct()
{
}
public function test()
{
return $data->get();
}
}
$system = new System();
echo $system->test();
So i was wondering how do I get the $data variable to be defined in my class? My use of global seems to be incorrect, I also put the global declaration in the __contrust() function but that didn't work either.
Define the global variable within the function instead of the class:
public function test()
{
global $data;
return $data->get();
}
EDIT: Alternate idea:
class System
{
private $values;
private $thedata;
public function __construct($data)
{
$this->thedata = $data;
}
public function test()
{
return $this->thedata->get();
}
}
$data = new testClass();
$system = new System($data);
echo $system->test();
So i was wondering how do I get the $data variable to be defined in my class? My use of global seems to be incorrect, I also put the global declaration in the __contrust() function but that didn't work either.
If you really want to use bad global construction, you should do like this:
class System
{
private $values;
// removed global from here
public function __construct()
{
}
public function test()
{
// added global here
global $data;
return $data->get();
}
}
But OOP principles recommend us to use composition, not global variables. So you can pass the $data into your another class via constructor or via setter. Here's some code implementing both approaches:
class testClass {
public function get()
{
echo __CLASS__.'::'.__FUNCTION__;
}
}
class System
{
private $values;
private $data;
public function __construct(testClass $data = null)
{
if ($data) {
$this->data = $data;
}
}
public function setData(testClass $data)
{
$this->data = $data;
}
public function test()
{
return $this->data->get();
}
}
$data = new testClass();
// via constructor
$system = new System($data);
// or via setter
$system = new System;
$system->setData($data);
echo $system->test();
You could pass $data when you instantiate the class and then assign it in the constructor, which will make it available to all the methods of the class.
class System {
public $data;
public function __construct($data) {
$this->data = $data;
}
public function index() {
echo $this->data;
}
}
$data = 'foo';
$system = new System($data);
echo $system->index();
outputs 'foo';
First things first... This could just be a simple "bad PHP syntax" issue. Look for forgotten ; or in my case... Forgetting that functions actually need the word function : )

How to access a variable with random value inside a class?

class Session{
protected $git = md5(rand(1,6));
public function __construct($config = array())
{
//// some code
$ses_id = $this->git;
}
public function _start_session()
{
//code again..
}
}
Here I can't assign a random value like this to variable called git. How can I do this if it is possible?
That random value need to be first time generated value only till the time it converts to Null.
Perform random inside your constructor,
class Session{
protected $git;
public function __construct($config = array())
{
//// some code
$this->git = md5(rand(1,6));
$ses_id = $this->git;
}
public function _start_session()
{
//code again..
}
}
Try setting the value of your variable in your constructor.
constructor will run every time you create an instance of your class.
try this code:
class Session{
protected $git;
public function __construct($config = array())
{
//// some code
$this->git = md5(rand(1,6));
}
public function _start_session()
{
//code again..
}
}
:)
Declare a variable inside a class,initialize the variables in class inside a constructor, which sets the variables once the object for that class is declared anywhere in the code.
I updated this answer if you want to do not change your session variable on each constructor call then use the below procedure.
class Session{
protected $git;
public function __construct($config = array())
{
$this->git = md5(rand(1,6));
if(!isset($_SESSION['ses_id']))
{
$_SESSION['ses_id'] = $this->git;
}
}
public function _start_session()
{
//code again..
}
}
I hope this helps you.
Try this using a global variable to track the random number:
class Session{
protected $git;
public function __construct($config = array())
{
//// some code
if (!isset($GLOBALS['random_val'])) {
$GLOBALS['random_val'] = md5(rand(1,6));
}
$this->git = $GLOBALS['random_val'];
$ses_id = $this->git;
var_dump("Session ID: ".$ses_id);
}
public function _start_session()
{
//code again..
}
}
$ses1 = new Session(); // Outputs string(44) "Session ID: 1679091c5a880faf6fb5e6087eb1b2dc"
$ses2 = new Session(); // Outputs string(44) "Session ID: 1679091c5a880faf6fb5e6087eb1b2dc"

How to pass php variable into a class function?

I want to pass php variable $aa into a class function. I have read some articles
in php.net, but I still don't understand well. Can anyone help me put the variable into this class? thanks.
$aa='some word';
class Action {
private $_objXML;
private $_arrMessages = array();
public function __construct() {
$this->_objXML = simplexml_load_file($aa.'.xml');
}
}
Simply put the variable names in the constructor.
Take a look at the snippet below:
public function __construct( $aa )
{
// some content here
}
I'm not sure what you mean... do you mean you want to access $aa in a function? If so:
$aa='some word';
class Action {
private $_objXML;
private $_arrMessages = array();
public function __construct() {
global $aa;
$this->_objXML = simplexml_load_file($aa.'.xml');
}
}
Or, on a per instance basis, you can do things like:
$aa='some word';
class Action {
private $_objXML;
private $_arrMessages = array();
public function __construct($aa) {
$this->_objXML = simplexml_load_file($aa.'.xml');
}
}
new Action($aa);
$aa='some word';
class Action {
private $_objXML;
private $_arrMessages = array();
public function __construct($aa) {
$this->_objXML = simplexml_load_file($aa.'.xml');
}
}
And use it like this:
$instance = new Action('something');
I don't know php, but my logic and google say this:
class Action {
private $_objXML;
private $_arrMessages = array();
public function __construct($aa) {
$this->_objXML = simplexml_load_file($aa.'.xml');
}
}
$object = new Action('some word');
This is simply called pass a variable as parameter of a function, in this case the function is the constructor of Action

Smarty custom plugin to use base class

Here is my class that gets called on each page:
class ActionHandler {
var $smarty = NULL;
public function __construct() {
if($this->smarty == NULL){
$this->smarty = new Smarty();
$this->smarty->template_dir = TEMPLATE_DIR;
$this->smarty->compile_dir = COMPILE_DIR;
}
}
public function do_something($page_id) {
return $page_id + 1;
}
}
Now I have a custom plugin for smarty that I want to use in my template:
function smarty_function_something($params, &$smarty) {
return ActionHandler::do_something($params['page_id']);
}
However I get Fatal error: Using $this when not in object context.
I see why but don't know how to get around this. Any ideas?
Try making the do_something a static member of ActionHandler
class ActionHandler {
public static $smarty = NULL;
public function __construct()
{
if($this->smarty == NULL)
{
$this->smarty = new Smarty();
$this->smarty->template_dir = TEMPLATE_DIR;
$this->smarty->compile_dir = COMPILE_DIR;
}
}
public static function do_something($page_id)
{
return $page_id + 1;
}
}
As your trying to access a non static method i *think that the __construct gets executed before the method is available, but as you have not created an instance of the object, the keyword $this does not exists.
you have to create specific static methods. if your going MyObject::SomeMethod($param)
you should also take a look at Object Auto Loading and Auto Initializing objects via static methods.
also you don't need to specifically define the value to public static $smarty = NULL; as Null is a default value of any new variable, just do
public static $smarty;
going a little more indepth with your problem you should add a singleton method like so..
class ActionHandler
{
public static $smarty;
public static $singleton;
public function __construct()
{
if($this->smarty == NULL)
{
$this->smarty = new Smarty();
$this->smarty->template_dir = TEMPLATE_DIR;
$this->smarty->compile_dir = COMPILE_DIR;
}
}
public static GetSingleton()
{
if(self::$singleton == null)
{
self::$singleton = new ActionHandler();
}
return self::$singleton;
}
public static function do_something($page_id)
{
$_this = self::GetSingleton();
return $page_id + 1;
}
}
You omitted a few pieces of code: instantiation of either the Smarty or ActionHandler object, registration of the template function, template content, and Smarty::display() call, but in my own testing your code works fine. In none of your code do you attempt to use $this while not in an object context.
If you have additional code to post (preferably, the full reduction that still triggers the error) that may help with debugging.
smarty-test.php:
<?php
include 'Smarty.class.php';
class ActionHandler {
var $smarty = NULL;
public function __construct() {
if($this->smarty == NULL){
$this->smarty = new Smarty();
$this->smarty->template_dir = __DIR__ . '/t';
$this->smarty->compile_dir = __DIR__ . '/tc';
$this->smarty->plugins_dir = __DIR__ . '/plugins';
}
}
public function do_something($page_id) {
return $page_id + 1;
}
}
$ah = new ActionHandler;
$ah->smarty->display('index.tpl');
plugins/function.something.php:
<?php
function smarty_function_something($params, &$smarty) {
return ActionHandler::do_something($params['page_id']);
}
t/index.tpl:
Test: {something page_id=1}
Output:
Test: 2

Categories