Welcome Guest, Not a member yet? Register   Sign In
How do I have a global array in __construct() ?
#2

I would advise you, stop what you are doing and please read up on basic OOP and PHP.
What you are asking is basic OOP knowledge. If you don't have that you are going to keep making the same mistakes over and over without even knowing it.

To help you out now
You declare global variables outside of any method. That includes the constructor!

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Main extends MY_Controller {

 
   // Declare all global variables here 
 
   public $notification = array();
 
   public $data;
 
   public $me;

 
   public function __construct()
 
   {
 
       parent::__construct();
 
       $this->load->library('session');
 
       $this->load->helper('url');

 
       if( isset($this->me) ) { $data['me'] = $this->me$me $this->me; }

 
       $this->notification $this->db->query("SELECT * FROM posts WHERE noti_recipient = '$me[id]' && noti_read = 'no' && userid != '$me[id]' ORDER BY datetimesecondnumber DESC LIMIT 1 ")->row_array();
 
       if( isset($this->notification) ) { $this->data['notification'] = $this->notification$this->me $this->notification; } // WHY???

 
   

Your code is very messy. Look into using models. You shouldn't write queries in the constructor of a Controller
Reply


Messages In This Thread
RE: How do I have a global array in __construct() ? - by Martin7483 - 08-03-2017, 06:15 AM



Theme © iAndrew 2016 - Forum software by © MyBB