0

I need some help with my php code. This is my code

class myclass extends anotherclass {

    var $bAlert;

    function myclass($var) {
        parent::anotherclass($var);
        $this->bAlert = false;
    }

function alert() {
        $this->bAlert = 'true';
        return;
    }

function display() {
        if(!$this->bAlert) {
           return;
        }
        return 'blah blah';
    }
}

Now what i want, I have a function to show something on screen when display() is called thats not the problem but i only want it to show up after alert() is called. So here is what i thought but it has a problem. I want to permanently change the value of $bAlert to true once alert() is called and of course it doesn't happen. So, anybody got any other bright ideas or any other way to do it?

Thanks

Prashank
  • 796
  • 6
  • 12
  • SO why you dont put $this->alert() at the first line of display() function ? This way you will always run alert() when you run display() – Svetoslav Nov 03 '12 at 08:16
  • Sure you mean `'true'` and not the constant `true` inside the `alert()` function? – arkascha Nov 03 '12 at 08:18
  • And it looks like you use a really outdated php version (or at least its syntax). Since version 5 constructors are called `__construct()` and not by the class names any more. – arkascha Nov 03 '12 at 08:20
  • nop i need to call this alert() from outside. For example. A user logs in and i call alert() and when the page loads i called display(). So, it will only showup when the user logged in. – Prashank Nov 03 '12 at 08:21
  • sorry about the 'true' its true actually. Its a cms so i have to follow code convection and have to use class names. – Prashank Nov 03 '12 at 08:23
  • Not sure what problem you face, actually. Calling `alert()` certainly _does_ change the value of `bAlert` inside the object `alert()` is called on. Might it be you want to have the value changed in _all_ objects of class myclass? Then you should take a look at static variables. Static variables are shared between all instanciated objects of a class. – arkascha Nov 03 '12 at 08:29

2 Answers2

2

Use singleton classes

Please visit here for more info on singleton classes

EDIT:

Or you can use static variables and methods

EDIT:

See this code:

<?php 
class myclass extends anotherclass {
    static public $bAlert;
    function myclass($var) {
        parent::anotherclass($var);
        self::$bAlert = false;
    }
    static function alert() {
        self::$bAlert = 'true';
        return;
    }
    function display() {
        if(!self::$bAlert) {
        return;
        }
        return 'blah blah';
    }
}
Community
  • 1
  • 1
Vipin Jain
  • 1,382
  • 1
  • 10
  • 19
  • if it will be only used within this class, you might want to consider changing `pulic static $bAlert` to `private static $bAlert`, though some frameworks don't work well with that. @Prashank You mentioned earlier that $bAlert can't be a boolean because of it's within a frame work? Then why you compare it like an boolean? a `String` will always be **`true`**. – Ariaan Nov 03 '12 at 08:49
  • hmm! this didn't worked for me and maybe the reason is still same. calling the class from anywhere sets it to false again. I will go with the temporary cookie way. I was trying to avoid it but looks like its the only option now. But many thanks for your reply, its very much appreciated. – Prashank Nov 03 '12 at 08:53
1

Ok, I'll add my implementation for clarity

Note: For this to work, you need to use session_start(); in all the script-pages you need the user to be logged-in.

class MyClass extends AnotherClass
{
  public static
    $bAlert = false;

  // This is the magic you need!
  public function __construct()
  {
    // Check if the session-var exists, then put it into the class
    if ( array_key_exists('bAlert', $_SESSION) ) {
      self::$bAlert = $_SESSION['bAlert'];

    // Set session with the default value of the class
    } else {
      $_SESSION['bAlert'] = self::$bAlert;
    }
  }

  function setAlert($alertValue = true)
  {
    // Used type-juggle in-case of weird input
    self::$bAlert = (bool) $alertValue;

    // Not needed, but looks neat.
    return;                 
  }

  function display($message = 'Lorum ipsum')
  {
    // This part will **only** work when it's a boolean
    if ( !self::$bAlert ) {
      return;
    }

    return $message;
  }
}

Btw, if you use classes in PHP5+ try using function __construct(){} instead of function MyClassName(){}. I know it looks weird compared with other programming-languages, but in PHP5+ it just works better.

For a better understanding of Classes & Objects and Sessions, this documentation might be useful:

Ariaan
  • 1,105
  • 1
  • 9
  • 13
  • 1
    After reading your question again, I'll also add a small implementation of Sessions – Ariaan Nov 03 '12 at 09:19
  • Actually i already used my cookie way to do this and i doubt i can add session_start() in a CMS cuz it has its own session system but i really appreciate your help. Thank you again. – Prashank Nov 03 '12 at 10:38
  • 1
    oh, that's pretty easy to replace with cookies. Anyway, be careful with cookies, there are a lot of countries with specific law's about do's and dont's. Just replace `$_SESSION` with `$_COOKIE` and you also don't need to worry about the `session_start()` then. Also keep in mind that cookies are browser depended, this could break a consistant user-experience of the overall site/application – Ariaan Nov 03 '12 at 10:44