Re: PHP5 Class problem
| From: | Jochem Maas | Date: | Fri, 28 Jan 2005 14:05:31 +0000 |
| Subject: | Re: PHP5 Class problem | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-207588@lists.php.net to get a copy of this message | ||
electroteque wrote:
On 29/01/2005, at 12:02 AM, Jochem Maas wrote:no abstract as in 'cannot be instantiated' try running the following: abstract class MyClass { public function __construct() { echo "boo!"; } } $mc = new MyClass; the 'final' keyword is used to declare that a class or method cannot be overridden by (or in) a subclass.abstract as in it is the final base class ?classes cannot be defined as static - defining them as abstract has the effect of being able to only use a given class statically (unless you subclass it and the subclass is not abstract).
difficult question. a start would be to say that static methods offer a neat way to organise your code (sort of cheapmans namespace). PHP5 also offers static class vars: abstract class MyClass { static private $val; static public function set($v) { self::$val = $v; } static public function speak() { if (!is_null(self::$val)) { echo self::$val."\n"; } else { echo "<silence>\n"; } } } MyClass::set("yeah"); MyClass::speak();i meant member vars , what is the point of static methods anyway ?variables inside it, or its only meant to stay in the one static method soyes you can use variables - but not member variables because $this is not defined in functions that are declared static - bare in mind you can call a method statically even though its not marked as static (just be sure you don't reference $this).
good ;-) ... the reason it doesn't work is the same reason PHP5 objects are soooo much cooler, namely a rewritten object model. thank the gods!sidenote: the php4 trick of overwriting $this does not work in PHP5.that i cant live with