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.

Empfohlene Antworten

Veröffentlicht

Hallo,

ich stehe irgendwie gerade auf dem Schlauch ...

Ich habe eine Config.php Klasse, in der einfach nur public Variablen drin stehen(sind eigentlich andere Variablen)

<?php
class Config{
public $dbhost="bliblablub";
public $dbdatabase="blubdatabase";
public $dbuser="blubadmin";
public $dbpassword="blubpassword";
}
?>[/PHP]

Diese Klasse steht in der Config.php Datei

Nun möchte ich diese Klasse in einer anderen Klasse aufrufen und diese jeder internen Methode zugänglich machen...

[PHP]<?php

include_once("conf/Config.php");
$foo=new Foo();
class Foo{
private $_configF=new Config();//<-funkt net
public function __construct(){
$_configF=new Config();
echo $_configF->dbhost;//<-funkt
$this->load();
}
private function load(){
echo $_configF->dbhost;//<-funkt net
}
}
?>

Wenn ich die Variable im Konstruktor definiere (oder irgendwo anders) ist diese nur dort initialisiert und nicht in der darauf folgenden Methode.

Ich arbeite nicht oft mit php und es ist wahrscheinlich ein dummer Fehler, aber selbst google hilft mir da wenig...

Wie also rufe ich den Konstruktor schon bei der Variablendefinition auf? Bzw initialisiere die Variable als Instanz der Klasse Config?

Warum machst du nicht so?


class Config
{
const DB_HOST = "bliblablub";
const DATABASE = "blubdatabase";
const DB_USER = "blubadmin";
const DB_PW = "blubpassword";
}

[/PHP]

Jetzt kannst du in einer Funktion z.B. sagen

[PHP]

public function connectDb()
{
mysqli_connect(Config::DB_HOST, Config::DB_USER, Config::DB_PW, Config::DATABASE);
}

Musst halt nur vorher die Config.php inkludieren.

Heyho,

versuch das mal:

<?php 

include_once("conf/Config.php");
$foo=new Foo();
$foo->test();
class Foo{
private $_configF;
public function __construct(){
$this->_configF=new Config();
}
private function load(){
echo $this->_configF->dbhost;
}
public function test(){
echo $this->_configF->dbhost;
}
}
?>[/PHP]

Du definierst die Variable in deiner Klasse als private und erzeugst dann im Konstruktor eine Instanz von der Klasse Config.

So müsste es gehen.

Grüße

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

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.