Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[PHP] Funktions- und Klassenname innerhalb der Funktion abfragen

Empfohlene Antworten

Veröffentlicht

Hi,

gibt es eine Möglichkeit den Funktionsnamen

in der Funktion selbst auszugeben ??


function meineFunktion($meinString)
{
$meinString .= nameDieserFunktion();
return $meinString;
}
[/PHP]

2. kann ich innerhalb einer Methode in einer Klasse mir den Klassennamen ausgeben lassen ?

[PHP]
class MeineKlasse
{
function MeineKlasse($meinString)
{
$meinString .= nameDieserKlasse();
return $meinString;
}
}

Ich benutze PHP Version 4.3.9

Danke für jede Hilfe !!!

Gruß Scratch

Hi!

Soweit ich weiß gibt es keine Funktion dafür und ich wüßte auch nicht wofür man so eine Funktion bräuchte.

Gruß, Tobias

wofür soll denn das gut sein, wo du doch die funktion über ihren namen aufrufen musst, also der name zum zeitpunkt des aufrufens immer bekannt ist ?

Hi

sicher geht das....

siehe:

http://www.dynamicwebpages.de/php/language.constants.predefined.php


function meineFunktion($meinString)
{
$meinString .= __METHOD__; // Ab PHP5
return $meinString;
}
[/PHP]

2. kann ich innerhalb einer Methode in einer Klasse mir den Klassennamen ausgeben lassen ?

[PHP]
class MeineKlasse
{
function MeineKlasse($meinString)
{
$meinString .= __CLASS__; // Ab PHP4.3.0
// oder $meinString .= get_class($this);
return $meinString;
}
}

bei der 2. Version musst du nur erst festlegen was bei abgeleiteten Klassen passieren soll.

Note that the constant __CLASS__ is different from get_class($this) :

<?

class test {

function whoami() {

echo "Hello, I'm whoami 1 !\r\n";

echo "Value of __CLASS__ : ".__CLASS__."\r\n";

echo "Value of get_class() : ".get_class($this)."\r\n\r\n";

}

}

class test2 extends test {

function whoami2() {

echo "Hello, I'm whoami 2 !\r\n";

echo "Value of __CLASS__ : ".__CLASS__."\r\n";

echo "Value of get_class() : ".get_class($this)."\r\n\r\n";

parent::whoami(); // call parent whoami() function

}

}

$test=new test;

$test->whoami();

$test2=new test2;

$test2->whoami();

$test2->whoami2();

?>

The output is :

Hello, I'm whoami 1 !

Value of __CLASS__ : test

Value of get_class() : test

Hello, I'm whoami 1 !

Value of __CLASS__ : test

Value of get_class() : test2

Hello, I'm whoami 2 !

Value of __CLASS__ : test2

Value of get_class() : test2

Hello, I'm whoami 1 !

Value of __CLASS__ : test

Value of get_class() : test2

In fact, __CLASS__ returns the name of the class the function is in and get_class($this) returns the name of the class which was created.

wofür soll denn das gut sein, wo du doch die funktion über ihren namen aufrufen musst, also der name zum zeitpunkt des aufrufens immer bekannt ist ?

Es kann z.B. redundanz vermieden werden:


class abc {
public function def( $array, $index) {
echo $array[$index];
__METHOD__( $array, $index+1);
}
}
[/PHP]

so wird die funktion rekursiv aufgerufen, aber falls du den Methoden-Namen ändern musst, musst du diesen nur an einer Stelle ändern!

Gruß,

Markus

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.