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

6
  • 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() Commented Nov 3, 2012 at 8:16
  • Sure you mean 'true' and not the constant true inside the alert() function? Commented Nov 3, 2012 at 8: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. Commented Nov 3, 2012 at 8: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. Commented Nov 3, 2012 at 8:21
  • sorry about the 'true' its true actually. Its a cms so i have to follow code convection and have to use class names. Commented Nov 3, 2012 at 8:23

2 Answers 2

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';
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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:

3 Comments

After reading your question again, I'll also add a small implementation of Sessions
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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.