我正在解决一个问题。我需要使用PHP将一个类对象传递给另一个类函数。我在下面解释我的代码。
db.php中:
class DBOperations{
// constructor
private $conn;
function __construct() {
$this->connect();
}
// destructor
function __destruct() {
// $this->close();
}
public function prepare_param(){
echo 'test';
}
}
$dbo = new DBOperations();
newclass.php:
require_once('db.php');
class DBhandler
{
function __construct()
{
}
function __destruct() {
// $this->close();
}
public function select_all_profile(){
$output=$dbo->prepare_param();
}
}
$dbh = new DBhandler();
print_r($dbh->select_all_profile());exit;
这里我需要将DBOperations
对象(i.e-$dbo
)传递给DBhandler
类函数来执行未发生的第一个类函数。请帮我解决这个问题。
答案 0 :(得分:0)
存储变量时,Require_once在重复功能中可能无法正常工作。
要确保每个函数调用都有可变条,请将require替换为require。例如情况:link
答案 1 :(得分:0)
TRIED&测试。
1st:第1班的public class AuthenticatedActivity extends AppCompatActivity {
private final static String TAG = "AuthenticatedActivity";
private static final int RC_SIGN_IN = 123;
protected FirebaseUser firebaseUser;
private FirebaseAuth auth;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
auth = FirebaseAuth.getInstance();
firebaseUser = auth.getCurrentUser();
if (firebaseUser == null) {
GoogleSignInOptions options = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(BuildConfig.WEB_CLIENT_ID)
.requestEmail()
.build();
// Build a GoogleSignInClient with the options specified by gso.
GoogleSignInClient client = GoogleSignIn.getClient(this, options);
startActivityForResult(client.getSignInIntent(), RC_SIGN_IN);
}
}
public void onDestroy() {
super.onDestroy();
FirebaseAuth.getInstance().signOut();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
Log.w(TAG, "Google sign in failed", e);
// TODO: Handle error
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
final AuthenticatedActivity activity = this;
auth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "signInWithCredential:success");
activity.firebaseUser = auth.getCurrentUser();
} else {
Log.w(TAG, "signInWithCredential:failure", task.getException());
// TODO: Handle error.
}
}
});
}
}
行会出错。
$this->connect();
答案 2 :(得分:0)
您缺少变量范围问题。您在db.php中实例化streamsupport
,但此变量在另一个文件的类中不可用。
你应该做更好的实例化,而不仅仅是在文件中,但要解决你的问题,请将DBOperations()
对象传递到DBOperations
。
db.php中:
DBhandler()
newclass.php:
class DBOperations{
private $conn;
function __construct() {
$this->connect();
}
public function prepare_param(){
echo 'test';
}
}
$dbo = new DBOperations();
答案 3 :(得分:0)
这个常见的构造会将DBOperations值传递给DBhandler类的构造函数,因为它可以重复使用多次。这通常称为依赖注入(DI),用于配置具有任何依赖关系的类,而不是在每个类中创建它们。
require_once('db.php');
class DBhandler
{
private $dbo;
function __construct( $dbo )
{
$this->dbo = $dbo;
}
function __destruct() {
// $this->close();
}
public function select_all_profile(){
$output=$this->dbo->prepare_param();
}
}
$dbh = new DBhandler($dbo);
print_r($dbh->select_all_profile());
exit;