<?
class Contact {
//protected $message = "Hello World";
function __construct($message){
echo $this ->message;
}
function getName() {
}
}
$con = new Contact("Hello World");
?>
<form method="post" action="Contact.php">
<form method="post" action={$con->getName()}>
答案 0 :(得分:3)
A)你不编译PHP
B)$message
不是实例变量,它是一个参数:
function __construct($message){
echo $message;
}
将PHP方法调用注入HTML的方式:
<form method="post" action="<?php echo $con->getName(); ?>">
您评论的//protected $message = "Hello World"
是正确的。这样的事情是理想的:
class Contact {
protected $message = "Hello World";
function __construct($message = null){
$this->setMessage($message);
}
public function getMessage(){
return $this->message;
}
public function setMessage($message = null){
if($message !== null){
$this->message = $message;
}
}
}
像这样使用:
$con = new Contact();
$con->getMessage(); // "Hello World;
$con->setMessage("Goodbye World");
$con->getMessage(); // "Goodbye World"
$con2 = new Contact("Goodbye World");
$con2->getMessage(); // "Goodbye World"