当TestPage.php在浏览器中运行时,"尝试创建新的obj..."是回显的,但没有别的。构造函数没有被调用吗?
这不是这两个类的完整代码,但希望它足以让有人告诉我我哪里出错了......
测试页.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/plain; charset=UTF-8">
<title></title>
</head>
<body>
<?php
class MyClass {
$api_key = 'somestring';
$username = 'username';
echo 'trying to create new obj...';
$myObj = new MyClass($api_key, $username);
echo 'new obj created...';
...
?>
</body>
</html>
我的班级.class.php
<?php
class MyClass {
protected $_api_key;
protected $_username;
public function __construct($api_key, $username) {
echo 'entered constructor...';
$this->_api_key = $api_key;
$this->_username = $username;
echo 'leaving constructor...';
}
...
}
?>
你需要把它定义为一个类。那看起来像:
class MyClass {
protected $_api_key;
protected $_username;
public function __construct($api_key, $username) {
echo 'entered constructor...';
$this->_api_key = $api_key;
$this->_username = $username;
echo 'leaving constructor...';
}
}
只是将您拥有的代码放在文件中并命名它本身不会做任何事情。
此外,如果尚未包含该文件,则需要包含该文件。像这样:
include 'MyClass.class.php';
你需要
class
关键字来定义一个类,请 http://php.net/manual/en/language.oop5.basic.php 一些基本的例子
尝试
class MyClass
{
protected $_api_key;
protected $_username;
public function __construct($api_key, $username) {
echo 'entered constructor...';
$this->_api_key = $api_key;
$this->_username = $username;
echo 'leaving constructor...';
}
}