![]() |
[PHP] Konstante in Klasse
Ich habe hier eine PHP-Klasse mit einer Konstanten:
Code:
Wenn ich selbige aber benutzen will, sagt er mir, dass er sie nicht kennen würde:
class CreatePDF
{ const CORRECTION_FACTOR_MAXCELLROWS = 1.60; // just a workaround
Code:
private function getMaxTextLinesForRow($i, $trans)
{ return max( $this->pdf->getNumLines((string)$i+1, $this->colWidths[0]), $this->pdf->getNumLines($trans['Position'][(string)$i]['title'], $this->colWidths[1]), $this->pdf->getNumLines($trans['Position'][(string)$i]['description'], $this->colWidths[2]), $this->pdf->getNumLines($trans['Position'][(string)$i]['net_price'], $this->colWidths[3]), $this->pdf->getNumLines($trans['Position'][(string)$i]['amount'], $this->colWidths[4]), $this->pdf->getNumLines($trans['Position'][(string)$i]['amount']*$trans['Position'][(string)$i]['net_price'], $this->colWidths[5]), $this->pdf->getNumLines($trans['Position'][(string)$i]['tax_type'], $this->colWidths[6]), $this->pdf->getNumLines((1-$trans['Position'][(string)$i]['tax_type']/100)*$trans['Position'][(string)$i]['amount']*$trans['Position'][(string)$i]['net_price'], $this->colWidths[7]) ) * $this->CORRECTION_FACTOR_MAXCELLROWS; } Zitat:
|
Re: [PHP] Konstante in Klasse
Hallo,
so sollte es funktionieren: self::CORRECTION_FACTOR_MAXCELLROWS |
Re: [PHP] Konstante in Klasse
Imho müsste es doch
Code:
heißen und nicht
CreatePDF::CORRECTION_FACTOR_MAXCELLROWS
Code:
, oder?
$this->CORRECTION_FACTOR_MAXCELLROWS
|
Re: [PHP] Konstante in Klasse
Hm, das wäre aber komisch. Auf Variablen und Methoden der Klasse kann ich mit
Code:
zugreifen. Ich probiere es mal.
$this->...
|
Re: [PHP] Konstante in Klasse
Auf Konstanten greifst Du, wie die anderen schon erwähnt haben, mit self:: bzw. Klassenname:: zu.
Du hast auch Zugriff auf die Konstanten, wenn die Klasse nicht instantiiert ist. |
Re: [PHP] Konstante in Klasse
A-ha. Also ist das bei Konstanten anders als bei Variablen und Methoden? Wie krank ist das denn? :wall:
|
Re: [PHP] Konstante in Klasse
Ist es nicht so, dass $this-> auf die aktuelle Instanz der Klasse geht? Wenn Du hingegen auf die Klassenkonstante zugreifen willst, dann bewegst Du Dich aus Sicht von PHP außerhalb der aktuellen Instanz - somit ist die andere Notation korrekt. (Es ist syntaktisch das Gleiche, wenn Du in PHP auf Klassenmthoden zugreifen möchtest.)
|
Re: [PHP] Konstante in Klasse
Aber die Konstante soll eigentlich keine Klassen-Konstante seion, sondern eine ganz einfache private Konstante innerhalb der Klasse, die auch nur dort gültig ist.
PS: Ich kann das ganze im Moment gerade nicht testen, der DB-Server bei uns spinnt und scheint down zu sein. |
Re: [PHP] Konstante in Klasse
Zitat:
|
Re: [PHP] Konstante in Klasse
Also mit
Code:
geht es. Aber setze ich ein private davor
self::...
Code:
meint er:
private const CORRECTION_FACTOR_MAXCELLROWS = 1.60; // just a workaround
Zitat:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 05:50 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz