PHP - Class Global
i have two classes and I am trying to call one class in through another here is my code but my welcome is not showing at all ever when i change the var's to 1
Code: [Select] class site { function DisplayContent ($page) { global $Users; echo '<div align="center"> <div id="Header"> <div id="NavBar">'; if ($Users->logged == 1) { echo 'Welcome '. $Users->user .' - <a href="?page=viewreminders">View Reminders</a> - <a href="?page=addreminder">Add Reminder</a> - <a href="logout.php" id="Logout">Logout</a>'; } else { echo '<a href="?page=login">Login</a> - <a href="?page=register">Register</a> - <a href="?page=forgotpassword">Forgot Password?</a>'; } echo '</div> </div> <div id="Content">'; $this->ShowPage($page); echo '</div> <div id="Footer">Copyright © '.date('Y').'<br /> Coded and Designed by Chris Cloyd</div> </div>'; } } class users { var $logged; var $user; var $email; var $level; function LogIn ($name,$email,$level) { $this->logged = 1; $this->user = $name; $this->email = $email; $this->level = $level; } function LogOut () { $this->logged = "0"; $this->user = ""; $this->email = ""; $this->level = ""; } } Similar TutorialsThis topic has been moved to CSS Help. http://www.phpfreaks.com/forums/index.php?topic=356168.0 This topic has been moved to CSS Help. http://www.phpfreaks.com/forums/index.php?topic=356577.0 I have mysqli object in Database class base: [color=]database class:[/color] class Database { private $dbLink = null; public function __construct() { if (is_null($this->dbLink)) { // load db information to connect $init_array = parse_ini_file("../init.ini.inc", true); $this->dbLink = new mysqli($init_array['database']['host'], $init_array['database']['usr'], $init_array['database']['pwd'], $init_array['database']['db']); if (mysqli_connect_errno()) { $this->dbLink = null; } } } public function __destruct() { $this->dbLink->close(); } } Class derived is Articles where I use object dBLink in base (or parent) class and I can't access to mysqli methods (dbLink member of base class): Articles class: require_once ('./includes/db.inc'); class Articles extends Database{ private $id, .... .... $visible = null; public function __construct() { // Set date as 2009-07-08 07:35:00 $this->lastUpdDate = date('Y-m-d H:i:s'); $this->creationDate = date('Y-m-d H:i:s'); } // Setter .... .... // Getter .... .... public function getArticlesByPosition($numArticles) { if ($result = $this->dbLink->query('SELECT * FROM articles ORDER BY position LIMIT '.$numArticles)) { $i = 0; while ($ret = $result->fetch_array(MYSQLI_ASSOC)) { $arts[$i] = $ret; } $result->close(); return $arts; } } } In my front page php I use article class: include_once('./includes/articles.inc'); $articlesObj = new articles(); $articles = $articlesObj->getArticlesByPosition(1); var_dump($articles); [color=]Error that go out is follow[/color] Notice: Undefined property: Articles::$dbLink in articles.inc on line 89 Fatal error: Call to a member function query() on a non-object in articles.inc on line 89 If I remove constructor on derived class Articles result don't change Please help me This question may sound very lame... ... But I have never able to know how to define a variable that I can use in different functions in a same file. There is no class structure just a PHP file with some functions. Is it possible to use some type of variable that can be share by all functions and that variable should not be a session variable . Something like: <?php $my_global_var; function A() { // use $my_global_var here } function B() { // use $my_global_var here and should retain the last value } ?> Something like in VB6.... Thanks If a class has a constructor but also has a static method, if I call the static method does the constructor run so that I can use an output from the constructor in my static method? --Kenoli I have an existing instance of my class Database, now I want to call that instance in my Session class, how would I go about doing this? Hi Can you call Class A's methods or properties from Class B's methods? Thanks. Ok. I know you can pass the object of a class as an argument. Example: class A { function test() { echo "This is TEST from class A"; } } class B { function __construct( $obj ) { $this->a = $obj; } function test() { $this->a->test(); } } Then you could do: $a = new A(); $b = new B($a); Ok so that's one way i know of. I also thought that you could make a method static, and do this: (assuming class A's test is 'static') class B { function test() { A::test(); } } But that is not working. I'd like to know all possible ways of accomplishing this. Any hints are appreciated. thanks Just wanted to know if $_Session was a global thing or does php require cookies setup?
I assumed that I could do something like this:
PAGE: A.php session_start(); $_Session['Cat'] = 'Meow'; // ----------------------- PAGE: B.php if (isset($_Session['Cat']) && !empty($_Session['Cat'])){ echo $_Session['Cat']; } OUTPUT: Meow // ----------------------- PAGE: C.php if (isset($_Session['Cat']) && !empty($_Session['Cat'])){ echo $_Session['Cat']; } OUTPUT: MeowI can't seem to get this to work. Page: Handler.inc // ========================================================= Session Log in / out public function logon($username, $password){ if ($username == "admin" && $password == "coffee"){ session_start(); $_SESSION['USER_STATUS'] = '1'; echo 'IN SESSION LOGIN HANDLER'; echo $_SESSION['UserLogged']; } } public function logout(){ $_SESSION['USER_STATUS'] = '0'; echo 'IN SESSION LOGOUT HANDLER'; session_destroy(); //session_unset(); }Page: index.php if (!isset($_SESSION['USER_STATUS']) && empty($_SESSION['USER_STATUS']) || $_SESSION['USER_STATUS'] == 0){ if (isset($_POST['username']) && !empty($_POST['username']) && isset($_POST['password']) && !empty($_POST['password'])){ $username = $_POST['username']; $password = $_POST['password']; echo $handler->logon($username, $password); } <LOGIN FORM CODE> }else{ if (isset($_SESSION['USER_STATUS']) && !empty($_SESSION['USER_STATUS']) || $_SESSION['USER_STATUS'] == 1){ echo 'Session name: '. $_SESSION['USER_STATUS']; } if (isset($_POST['btn_logout']) && !empty($_POST['btn_logout'])){ $handler->logout(); echo '<br />Logged out<br />'; } <LOGOUT FORM BUTTON CODE> }Could someone point me in the right direction please. It does not seem to hold its session. It's a function=> Code: [Select] function _wp_call_all_hook($args) { global $wp_filter; //var_dump($wp_filter); //exit(); reset( $wp_filter['all'] ); do { foreach( (array) current($wp_filter['all']) as $the_ ) if ( !is_null($the_['function']) ) call_user_func_array($the_['function'], $args); } while ( next($wp_filter['all']) !== false ); } Why is $wp_filter variable declared as global ? Was $wp_filter assigned before ? hello, ok if i set a Global on index.php (wwwroot/admin/index.php) it works Code: [Select] $page1='hello from page1 on index'; global $page1; echo $page1; i can echo $page1 on other pages like (wwwroot/admin/pages/home.php) and it will echo out "hello from page1 on index". GREAT but if i write a Global on (wwwroot/admin/pages/home.php) it does not work. i also can not echo it out on any other page Code: [Select] $page2='hello from page2 on home'; global $page2; echo $page2; why is this ??? I use $db object to handle database function like $db->get_row for example. Is there any way that I don't need to put into every function global $db or do I actually need to repeat in every function global $db? Code: [Select] function example(){ global $db //Do I need to repeat this in each function? } I have some variables that I have set with the global command but they don't seem to be working correctly. It's probably my order of operation but not sure. Here's the stripped down version of whats going on: locator() //functions function geoip() global $zip, $city, $areacode, $metrocode, $state, $country, $latitude, $longitude; function locator() geoip() use globals from geoip pass to- ad_display() function ad_display() does some work stat_tracking() function stat_tracking() access global $zip, $city, $areacode, $metrocode, $state, $country, $latitude, $longitude; The problem is once I call stat_tracking() and try to access the global variables, they are all empty. Just to clarify the chain of events is: page loads, calls locator(), locator() calls function geoip() fine then passes work to ad_display(), ad_display() calls stat_tracking(), stat_tracking() now tries to access the globals I made but all variables are blank if I echo them. Any ideas? Hey, I've been developing a browsergame. In this you are able to produce something ("weed"). Now I've got some kind of trouble with the function that is responsible for updating the "weed" on the user accounts. Im going to explain, how my script is working First step: Loading the datas from "worlds". In this case id, weed_factor, weed_basis. What this is required for you will see in the next steps <?php include 'includes/settings/mysql.php'; mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbbase); $time = time(); $update_weed_world_data_sql = "SELECT id, weed_factor, weed_basis FROM worlds"; $update_weed_world_data_res = mysql_query($update_weed_world_data_sql) OR DIE (mysql_error()); while($update_weed_world_data_while = mysql_fetch_assoc($update_weed_world_data_res)){ $update_weed_world_id = $update_weed_world_data_while['id']; $update_weed_world_basis[$update_weed_world_id] = $update_weed_world_data_while['weed_basis']; $update_weed_world_factor[$update_weed_world_id] = $update_weed_world_data_while['weed_factor']; global $update_weed_world_basis; } Second step: This is the function which is editing each account by the data given as parameters. function do_update_weed($profile_id,$world,$update,$level,$time){ $update_weed_period = $time - $update; $update_weed_add = ($update_weed_period / 3600 * $update_weed_world_basis[$world] * pow($update_weed_world_factor[$world],$level)) +1; echo $update_weed_world_basis[$world]; echo "<br>"; mysql_query("UPDATE profiles SET weed = weed +$update_weed_add, weed_update = $time WHERE id = $profile_id LIMIT 1"); } Note: echo $update_weed_world_basis[$world]; doesnt result an output. In step three, the accounts are loaded: function todo_update_weed($time){ $todo_update_weed_sql = "SELECT * FROM profiles WHERE weed_update < $time"; $todo_update_weed_res = mysql_query($todo_update_weed_sql) or die (mysql_error()); while($todo_update_weed_while = mysql_fetch_assoc($todo_update_weed_res)){ do_update_weed( $todo_update_weed_while['id'], $todo_update_weed_while['world_id'], $todo_update_weed_while['weed_update'], $todo_update_weed_while['level_farm'], $time ); } } And the last step is calling function "todo_update_weed" with the time. todo_update_weed($time); I script calculate the time between the last update and the current time. This make a difference in time. With this value I calculate something else. And then the update time has to be saved. The script has just working fine, when I set the variable "$update_weed_add" with a fix value. But by now the script doesn't work, because I don't know how to make the array available inside functions. I considered whether I put the database-function for the world_data inside the function that updates the profiles. But if I do this, the database will be called thousands of times... Hope anybody has a nice idea to fix the problem hello. I have some oop code that pulls information about a page out of a mysql db but i dont want to put the code on every page. Instead in want to put it in the a function and pass it back to each page. do i put it in a function in my functions.php file or can i put it into my includes/pages.php includes file ?? the code looks for the page name on the page and then pulls that pages info from the db. this code works on the page - home.php Code: [Select] <?PHP require_once("../includes/initialize.php"); $currentPages = Pages::find_all(); $pName = "adminHome"; $page_id = ""; $visible = ""; $pageZones = ""; $pageCRUD = ""; $pageTypes = ""; $module = ""; $title = ""; $sub_title = ""; $description = ""; $image = ""; $about = ""; foreach($currentPages as $currentPage){ $page_id = $currentPage->id; $pageName = $currentPage->pageName; $visible = $currentPage->visible; $pageZone = $currentPage->pageZones_id; $pageCRUD = $currentPage->pageCRUD_id; $pageType = $currentPage->pageTypes_id; $module = $currentPage->module_id; $title = $currentPage->title; $sub_title = $currentPage->sub_title; $description = $currentPage->description; $image = $currentPage->image; $about = $currentPage->about; if($pageName == $pName){ echo $pageName.'<br/>'; echo $page_id.'<br/>'; echo $visible.'<br/>'; echo $pageZone.'<br/>'; echo $pageCRUD.'<br/>'; echo $pageType.'<br/>'; echo $module.'<br/>'; echo $title.'<br/>'; echo $sub_title.'<br/>'; echo $description.'<br/>'; echo $image.'<br/>'; echo $about.'<br/>'; } } ?> i would like to be able to just put global $page or something like that to get all the db info for each page. i tried this in my includes/pages.php includes file but it cant get the page name. Code: [Select] public static function find_by_pageName(){ global $database; global $pName; $sql = "SELECT * FROM ".self::$table_name." WHERE pageName=".$pName.""; $result_array = self::find_by_sql($sql); return !empty($result_array) ? array_shift($result_array) : false; } any suggestions ???? whats the best way to do this ? thanks ricky Hiya! I was wondering if this is possible. Here is my example. This is a example class; <?php class a { public function __construct() { } public function returnValue() { include 'files/lang.php'; $this->lang = new lang(); return $this->lang->displayLang('10'); } } ?> This is the second class file; <?php class b { public function __construct() { } public function returnValue() { include 'files/lang.php'; $this->lang = new lang(); return $this->lang->displayLang('10'); } } ?> This is the main file; <?php include 'test/a.php'; $test = new a(); $test->returnValue(); include 'test/a.php'; $test2 = new b(); $test2->returnValue(); ?> With this I'm getting an error stating I cannot redeclare the function lang. I have found a way around this by using require_once but I was wondering if remove the include and $this->lang var from class 2 and I put this as the $this->lang var declaration in class 1; global $this->lang = new lang(); will class 1 and class 2 be able to use it from only the one declaration made in class 1. If so, would this be the way to do it or is there a better/working way to do it? Many many thanks, James. ok iv set up a simple demo this is the code im running include('simple_html_dom.php'); include('config.php'); include('connect.php'); include('functions.php'); include($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'globalFunctions.php'); test(); echo "test variable = ".$test; exit; this is the function which is in globalFunctions.php which is two directories back ../../ function test() { $test = 10; global $test; } how come when i run script i am not getting variable echoed Hi there, I am a highly concerned about ever using global variables (as many guides I have seen on security issues with PHP say you should avoid them, kind of understand why though to be fair), but I was wanting to really understand custom based session handlers in PHP and this first one he http://uk.php.net/manual/en/function.session-set-save-handler.php on the example: 'Example #1 session_set_save_handler() example' uses them. Now would it be better to take the global variable in function: ''open()' and then return the variable $sess_save_path then pass it up to the next function that may need it perhaps? I just don't want to use globals if I can avoid it, been working out a system that work use for some old solution we use and it uses globals and it really bugs the living life out of me, why people are so lazy they can't avoid them at all costs grrr. Any advice generally speaking is of great appreciation, Jeremy. |