Autor Beitrag
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8535
Erhaltene Danke: 473

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mo 21.02.11 22:46 
Ist doch etwas viel für die SB, und das möchte ich jetzt mal wissen. :gruebel:

Ich hab da folgendes in einer Klasse:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
public function Foo()
{
  return FALSE;
}
// ..
$myVar = $this->Foo;
if (!myVar) // muss natürlich heißen: if (!$myVar)
{
  echo 'Hallo Welt';
}

Warum ist sowas syntaktisch korrekt? Was macht PHP da, also wie interpretiert der das "myVar", was ja nicht "$myVar" ist? Würde ich gerne verstehen, damit die nächste Suche nach einem fehlenden $ schneller beendet ist ... :lupe:


Moderiert von user profile iconChristian S.: Topic aus Delphi4PHP-Technologien verschoben am So 15.07.2012 um 13:11

_________________
We are, we were and will not be.
Crombo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 54
Erhaltene Danke: 8

Windows 7, Windows Vista, Windows XP
Delphi XE Architect, Visual Studio 2010 Professional
BeitragVerfasst: Mo 21.02.11 23:00 
Hallo,

schau dir mal mit error_reporting(E_ALL) die Notices. An. PHP nimmt da nämlich an, dass du eine Konstante auf true checkst und bekommt von der Variable gar nichts mit.

Crombo

_________________
one day your life will flash before your eyes. make sure it's worth watching

Für diesen Beitrag haben gedankt: Gausi
Gausi Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8535
Erhaltene Danke: 473

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mo 21.02.11 23:11 
Ah, dann wird das "myVar" als Konstante angesehen, und weil die nicht definiert ist, wird die als 0 angenommen, also ist !myVar falsch, also wird das "Hallo Welt" nicht ausgegeben.

Ok, das wär dann wohl geklärt. Aber irgendwie ist mir Pascal da lieber. Da fliegt einem sowas nämlich mit ner anständigen Fehlermeldung um die Ohren. :?

_________________
We are, we were and will not be.
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mo 21.02.11 23:17 
user profile iconGausi hat folgendes geschrieben Zum zitierten Posting springen:
wird die als 0 angenommen, also ist !myVar falsch
Sicher? :zwinker:

IIRC hat die Konstante myVar dann den Wert "myVar", aber ich hab's jetzt nicht nochmal nachgeguckt.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Gausi Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8535
Erhaltene Danke: 473

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mo 21.02.11 23:22 
Ja, kann auch gut sein. "0" wäre ja auch False, und somit !myVar wieder True. Der Wert "myVar" ist dann aber True, oder?

Ich hab mich ja manchmal über die superstrenge Typsicherheit von Delphi geärgert, aber was PHP da macht ist doch irgendwie auch Kappes ...

_________________
We are, we were and will not be.
Dude566
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Mo 21.02.11 23:32 
Einfach an die Syntax halten dann kann auch nichts passieren. :P
Ist da aber schon manchmal ärgerlich wenn man einen doofen Fehler nicht sofort entdeckt. :autsch:

_________________
Es gibt 10 Gruppen von Menschen: diejenigen, die das Binärsystem verstehen, und die anderen.
Jakob_Ullmann
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1747
Erhaltene Danke: 15

Win 7, *Ubuntu GNU/Linux*
*Anjuta* (C, C++, Python), Geany (Vala), Lazarus (Pascal), Eclipse (Java)
BeitragVerfasst: Sa 05.03.11 14:58 
und das

ausblenden Quelltext
1:
$myVar = $this->Foo;					


nimmt er wirklich an? Wäre das nicht, wenn schon, ein Pointer auf Foo? Pointer scheint PHP ja zu kennen (lässt zumindest $this-> vermuten).
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 05.03.11 15:08 
"->" entspricht dem "." in Delphi, als einfach dem Zugriff auf die Member eines Objekts.

Da fehlt IMHO aber wirklich noch ein "()" für den Funktionsaufruf.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Jakob_Ullmann
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1747
Erhaltene Danke: 15

Win 7, *Ubuntu GNU/Linux*
*Anjuta* (C, C++, Python), Geany (Vala), Lazarus (Pascal), Eclipse (Java)
BeitragVerfasst: Sa 05.03.11 17:36 
OK, dann habe ich das falsch erkannt, weil in C und C++ myVar->doSomething() dasselbe wie (*myvar).doSomething() ist (ich habe lange nichts mehr mit PHP gemacht und ich vermisse es auch nicht wirklich).