Re: Classes within classes
| From: | Lars Torben Wilson | Date: | Sat, 28 Jun 2003 05:02:15 +0000 |
| Subject: | Re: Classes within classes | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-153244@lists.php.net to get a copy of this message | ||
On Fri, 2003-06-27 at 03:09, Dave Alger wrote:
> Hiya,
>
> It would be useful for me to be able to use a class within another class and
> I don't know if that's permissable in PHP.
Hi there!
It is indeed possible. However, that's not the problem here. The problem
is that class attribute initializers can only be constant values (i.e.
no variables or non-constant expressions). This is explained in the
manual's section on Classes and Objects:
http://www.php.net/manual/en/language.oop.php
Specifically, check out the note right after the Caution box.
> Here's an example of what I'm trying to do. Is it just simply not permitted
> or am I going about it the wrong way?
[snip]
This is a minor rewrite which shows one way to do what you want:
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
class CSSFont
{
var $family ='Arial';
var $size ='12pt';
var $style = 'none';
function Output()
{
$out = "\n";
$out .= "font-family:".$this->family.";\n";
$out .= "font-size:".$this->size.";\n";
$out .= "font-style:".$this->style.";\n";
return $out;
}
}
class CSSBorder
{
var $font1;
var $font2;
var $out = '';
function Output()
{
$this->out = $this->font1->Output();
$this->out .= " other stuff goes here ";
$this->out .= $this->font2->Output();
}
function Display()
{
echo $this->out;
}
function CSSBorder()
{
$this->font1 = new CSSFont;
$this->font2 = new CSSFont;
}
}
$border = new CSSBorder();
$border->Output();
$border->Display();
?>
> Thanks in advance for any help,
>
> Dave
Hope this helps clarify things,
Torben
--
Torben Wilson <torben@php.net> +1.604.709.0506
http://www.thebuttlesschaps.com http://www.inflatableeye.com
http://www.hybrid17.com http://www.themainonmain.com
-----==== Boycott Starbucks! http://www.haidabuckscafe.com ====-----