我如何在PHP __construct中执行Javascript函数

Well, i want to call a function like alert() (javascript) into the class construct PHP, when the POST variable is no define. but i don't no if it's posible.

class Su{

    private $nomb;
    private $dir;
    private $tel;

    public function __CONSTRUCT($mode){
        try {
            if(isset($_POST['nomb'])) {
                ?>
                <script language="javascript">alert("Sorry, this field can't be empty ")</script>
                <?php
                throw new Exception('?view=sucursal&mode=' . $mode . '&error=define');
            }
        } catch(Exception $error) {
            header('location: '.$error->getMessage());
            exit;
        }
        $this->nomb = strtoupper($_POST['nomb']);
        $this->dir = strtoupper($_POST['dir']);
        $this->tel = strtoupper($_POST['tel']);
    }

    public function __GET($var){ return $this->$var; }
    public function __SET($var, $valor){ return $this->$var = $valor; }

}

What you are asking for, is not what's good for you. You ought to think differently about how to achieve your goal.

php executes a script in less than 1 millisecond. It sits on the server, and only works when a client makes a request.

Javascript is different. It works on the computer of the client; it constantly waits for events (mouse clicks, keys pressed, timers, ...).

You ought to use javascript for event handling. There can be no reason for PHP to dynamically generate javascript code (in your case the alert). No, you make sure that alert is already in the javascript code, in some function. Then you let javascript call that function.

What you can do, is get a result from the server, and according to that result you let javascript decide to call that (or another) javascript function. This is what AJAX is about.

Do you know and use jQuery?

Here is an example. Let's say "ajax.php" prints the hour of the day.

<div id="welcome"></div>
<script>
  $.ajax({
    url: 'ajax.php',
    success: function(output) {
      var hour = Number(output);
      if(hour < 11) {
        good_morning();
      }
      if(hour >= 11 && hour < 14) {
        good_day();
      }
      if(hour >= 14 && hour < 18) {
        good_afternoon();
      }
      // etcetera  ...
    }
  });
  function good_morning() {
    $('#welcome').html('Good morning, what a loverly day ...');
  }
  function good_day() {
    $('#welcome').html('Good Day, ...');
  }
  // etcetera  ...
</script>