• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I have a global array in __construct() ?

#1
I have this code.


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

class Main extends MY_Controller {

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

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

$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)){ $data['notification'] = $this->notification; $me = $this->notification; }

}


How do I make the $notification variable defined in __construct() a global be passed to all methods in the main controller?

I tried using the public feature but it doesn't work with arrays.

Please help.
Reply

#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

#3
As @Martin7483 say's use models, think of the Controller as a switching station.


Also I find it best to use CodeIgniter's autoload to load the sessions and url helper
if you are using them all the time.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#4
I can only agree with Martin7483. Stop immediatly and start leaning to programm. Read some of the funamental tutorials and principals.
You are trying to run before you can even crawl. Just because you can drive a car does not mean you can start building a racing car.
On the package it said needs Windows 7 or better. So I installed Linux.
Reply

#5
(08-03-2017, 06:15 AM)Martin7483 Wrote: Your code is very messy. Look into using models. You shouldn't write queries in the constructor of a Controller

(08-04-2017, 08:51 AM)rtenny Wrote: I can only agree with Martin7483. Stop immediatly and start leaning to programm. Read some of the funamental tutorials and principals.
You are trying to run before you can even crawl. Just because you can drive a car does not mean you can start building a racing car.

I found a good tutorial on object orientated programming on envato net tuts which explains how to do it with more OOP concept.s

The thing with programming is that there is so much that one could possibly know about it, that one cannot learn it all, so I only learn the stuff that I am required to know.  Wink

I like to write database queries in the controller. I believe in following DRY. Don't Repeat Yourself. So I don't see the point of moving the database queries to the model if the code for the queries are only going to be used once in the code. It's not like I'm using the same block of code repeatedly.

Thank you for your help.
Reply

#6
(08-07-2017, 01:05 PM)desbest Wrote: I found a good tutorial on object orientated programming on envato net tuts which explains how to do it with more OOP concept.s

The thing with programming is that there is so much that one could possibly know about it, that one cannot learn it all, so I only learn the stuff that I am required to know.  Wink

I like to write database queries in the controller. I believe in following DRY. Don't Repeat Yourself. So I don't see the point of moving the database queries to the model if the code for the queries are only going to be used once in the code. It's not like I'm using the same block of code repeatedly.

Thank you for your help.

The fact that you don't even know how to set a public variable inside a class to be accessible by related objects,
indicates that you still doesn't know THE VERY BASIC of oop.
or, that "good tutorial" you're using as learning resource doesn't provide a good basic practice of oop.
or, you're the kind of person that likes to skim an article of tutorial, skipping important parts,
and brags that "i already learned the article, and its good".

writing queries inside controllers is... i asure you... a very-very bad practice.
your so-called 'DRY' isn't just talking about repeating the same-exact things over and over.
it's also a practice of how to better organize your source between app-logic and db-models in your app.


so I'm gonna agree with everyone else here,
STOP DOING WHAT YOU'RE DOING... and right now.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.