你好,請問要怎麼能讓class外部能改動class內部的變數?
我想要case_code::$case_code=$row["case_code"];
這一行改變case_code.inc.php
這個php檔中case_code
類別裡面的$case_code
這個變數。
但我印出get_case_code()
這個function時,它顯示 Undefined variable: case_cod
請問有什麼辦法嗎 謝謝
<?php
require_once("dbtools.inc.php");
require_once("case_code.php");
//指定每頁顯示幾筆記錄
$records_per_page = 5;
//取得要顯示第幾頁的記錄
if (isset($_GET["page"]))
$page = $_GET["page"];
else
$page = 1;
//建立資料連接
$link = create_connection();
//執行 SQL 命令
$sql = "SELECT * FROM peole_data ORDER BY date DESC";
$result = execute_sql($link, "makecase", $sql);
$total_records = mysqli_num_rows($result);
echo "記錄數".$total_records;
$total_pages = ceil($total_records / $records_per_page);
echo "總頁數".$total_pages;
$started_record = $records_per_page * ($page - 1);
mysqli_data_seek($result, $started_record);
$j=0;
while ($row = mysqli_fetch_assoc($result) and $j <= $records_per_page)
{
echo " <div class='case'>
<a class='case-link 'href='case.php'></a>
<div class='casetitle'>".$row["case_title"]."</div>
<div class='caseinner'>".$row["discritble_case"]."</div>
</div>";
case_code::$case_code=$row["case_code"];
$j++;
}
echo "</table>" ;
//產生導覽列
echo "<p align='center'>";
if ($page > 1)
echo "<a href='findcase.php?page=". ($page - 1) . "'>上一頁</a> ";
for ($i = 1; $i <= $total_pages; $i++)
{
if ($i == $page)
echo "$i ";
else
echo "<a href='findcase.php?page=$i'>$i</a> ";
}
if ($page < $total_pages)
echo "<a href='findcase.php?page=". ($page + 1) . "'>下一頁</a> ";
echo "</p>";
//釋放記憶體空間
mysqli_free_result($result);
mysqli_close($link);
?>
<?php
class case_code{
public static $case_code;
function get_case_code(){
return $case_code
}
}
?>
<?php
class case_code{
public static $case_code;
function get_case_code(){
return $this->case_code;
}
}
?>
再多學習一下吧。這是很基本的物件使用方式。
而且,你都宣告靜態了。你確定可以利用外部程式改值??
<?php
class case_code{
public static $case_code;
function get_case_code(){
return $this->case_code;
}
function set_case_code($case_code){
$this->case_code = $case_code;
}
}
$testData = "test";
case_code::set_case_code($testData);
echo case_code::get_case_code();
?>
上面的程式碼是很基本的應用。
在來是非靜態宣告式的可外部改值。
<?php
class case_code{
public $case_code;
function get_case_code(){
return $this->case_code;
}
}
$testData = "test";
$case_codeClass = new case_code();
$case_codeClass->case_code = $testData
echo case_code::get_case_code();
?>
還有其它許多方式,剩下來的自已研究。
我只說基本的。
好的謝謝
<?php
class case_code
{
public static $case_code;
public static function get_case_code(){
return self::$case_code;
}
}
case_code::$case_code = 'abc';
echo case_code::get_case_code();