Axel Richter: PHP meckert beim vorbelegen von variablen

Beitrag lesen

Hallo,

Oder: Richtige Syntax, falsche Sprache.
$reply_to="webmaster@".$_SERVER['SERVER_NAME'];
nicht ganz.... ich hab das ja innerhalb einer klasse stehen.
und da müssen variablen meines wissens mit einem "var" deklariert werden, oder?

http://www.php.net/manual/en/language.oop.php

In PHP 4, only constant initializers for var variables are allowed. To initialize variables with non-constant values, you need an initialization function which is called automatically when an object is being constructed from the class. Such a function is called a constructor (see below).

<?php
class Cart {
   /* None of these will work in PHP 4. */
   var $todays_date = date("Y-m-d");
   var $name = $firstname;
   var $owner = 'Fred ' . 'Jones';
   /* Arrays containing constant values will, though. */
   var $items = array("VCR", "TV");
}

/* This is how it should be done. */
class Cart {
   var $todays_date;
   var $name;
   var $owner;
   var $items = array("VCR", "TV");

function Cart() {
       $this->todays_date = date("Y-m-d");
       $this->name = $GLOBALS['firstname'];
       /* etc. . . */
   }
}
?>

viele Grüße

Axel