我尝试从 c_index.php
调用 m_newEmployee
时会遇到以下错误。
我尝试了:
$employee = new AppmodelnewEmployee;
,但这在m_newEmployee
中引起了另一个奇怪的错误。
当我这样做时,函数getEmployees()
不能用于创建任何对象。
c_index.php
<?php
namespace appcontroller;
use appmodel;
require_once "../task/vendor/autoload.php";
/**
*
*/
class c_index
{
function loadEmployees()
{
$employee = new m_newEmployee;
echo $employee->getEmployees();
}
}
?>
m_newemployee.php
<?php
namespace appmodel;
require_once "../task/vendor/autoload.php";
/**
*
*/
class m_newEmployee
{
function createTable()
{
$database = new m_database;
$conn = $database->connect();
// sql to create table
$sql = "CREATE TABLE IF NOT EXISTS employees (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
employee VARCHAR(255) NOT NULL,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";
if ($conn->query($sql) === TRUE) {
//echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
}
function createEmployee($employee)
{
$this->createTable();
$sql = "INSERT INTO `employees`( `employee`) VALUES ('$employee')";
if ($conn->query($sql) === TRUE) {
$result = true;
return $result;
} else {
echo "Error inserting " . $conn->error;
}
}
function getEmployees()
{
$database = new m_database;
$conn = $database->connect();
$sql = "SELECT * FROM `employees`";
if ($result = $conn->query($sql) === TRUE) {
//Initialize array variable
$dbdata = array();
//Fetch into associative array
while ( $row = $result->fetch_assoc()) {
$dbdata[]=$row;
}
//Print array in JSON format
return json_encode($dbdata);
}
else {
echo "Error inserting " . $conn->error;
}
}
}
composer.json
{
"autoload":{
"psr-4":{
"":"app",
"App\":"app"
}
}
}
致命错误:未被发现的错误:c: xampp htdocs task task app controller contex.php中找不到类" m_newemployee"。php(14(:app controller c_index-> loademployees((#1 {main}在c: xampp htdocs task task task app app contranter controlter c_index.php中,第16行16
>
导入命名空间(use appmodel;
(并不意味着您可以直接使用该名称空间中的模型。您需要指定命名空间(使用导入的简短名称(:
namespace appcontroller;
use appmodel;
// ...
$employee = new modelm_newEmployee();
或直接导入模型:
namespace appcontroller;
use appmodelm_newEmployee;
// ...
$employee = new m_newEmployee();