我在PHP中使用MVC模式。 MVC模式限制您在类中调用方法。 据我所知,我不能在课堂范围之外工作。
如何将变量传递给函数?我找到的唯一可以声明变量的地方是函数内部。这使得函数static
无法重复使用!
示例:
class usersModel {
function q(){
$sql = "SELECT * FROM users WHERE fname='eli'";
$result1 = $this->conn->query($sql);
$result2 = $result1->fetch_assoc();
$fname = ($result2['fname']);
return $fname;
}
}
之后,当我在另一个类中创建该类的对象时:
$model = usersModel();
$result = $model->q();
我将在哪里声明变量$sql
,以便我不必将其包含在函数中?
答案 0 :(得分:0)
你必须声明函数public然后你可以访问类之外的方法并将sql param传递给方法。
class usersModel {
public function q($sql){
$result1 = $this->conn->query($sql);
$result2 = $result1->fetch_assoc();
$fname = ($result2['fname']);
return $fname;
}
}
$sql = "SELECT * FROM users WHERE fname='eli'";
$user = new usersModel();
$user->q($sql);
答案 1 :(得分:0)
大家好,感谢您的投入!
这是我正在寻找的答案 - 我必须将变量作为类外的常量进行处理,以便类中的方法能够达到它!
Sub CreateWordDocument()
'Connect using Early Binding.
'Remember to set the reference to the Word Object Library
'In VBE Editor Tools -> References -> Microsoft Word x.xx Object Library
Dim WordApp As Word.Application
Set WordApp = New Word.Application
WordApp.Documents.Add Template:="Normal", NewTemplate:=False, DocumentType:=0
WordApp.Visible = True 'New Apps will be hidden by default, so make visible
'code copied from Excel Macro recorder
Sheets("Appointment letter").Select
Range("A1:O39").Select
Selection.Copy
'code copied from Word Macro recorder with WordApp. added to the front.
WordApp.Selection.PasteAndFormat (wdPasteDefault)
With The_WordDoc.Styles("Normal").ParagraphFormat
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
The_WordDoc.Styles("Normal").NoSpaceBetweenParagraphsOfSameStyle = _
False
With The_WordDoc.Styles("Normal")
.AutomaticallyUpdate = False
.BaseStyle = ""
.NextParagraphStyle = "Normal"
End With
Set WordApp = Nothing 'release the memory
End Sub