在我的模式文件中,我定义了一个具有可能元素序列的组.
<group name="argumentGroup">
<sequence>
<element name="foo" type="double" />
<element name="bar" type="string" />
<element name="baz" type="integer" />
</sequence>
</group>
Run Code Online (Sandbox Code Playgroud)
然后我像这样引用这个组:
<element name="arguments">
<complexType>
<group ref="my:argumentGroup"/>
</complexType>
</element>
Run Code Online (Sandbox Code Playgroud)
是否有可能在某个其他方面引用该组但限制它,因此它是一个选择而不是一个序列.我想重用它的位置只允许其中一个元素.
<element name="argument" minOccurs="0" maxOccurs="1">
<complexType>
<group name="my:argumentGroup">
<! -- Somehow change argumentGroup sequence to choice here -->
</group>
<complexType>
</element>
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习PHP OOP,并对如何在我的项目中使用全局数据库类进行了一些研究.从我所看到的,最合适的模式是一个单例,它可以确保始终只存在一个数据库连接.然而,由于这是我第一次使用Singleton模式,我不确定我是否做得对.
这是一个合适的单身人士吗?此代码是否仅确保一个数据库连接?有什么办法可以测试吗?(学会钓鱼的人,他将在余生中获得食物......)
我使用redbean作为我的ORM,这是我如何明确地设置它:
require_once PLUGINPATH.'rb.php';
$redbean= R::setup("mysql:host=192.168.1.1;dbname=myDatabase",'username','password');
Run Code Online (Sandbox Code Playgroud)
我已经基于这个源创建了以下脚本,作为我自己的单例数据库类;
class database {
private $connection = null;
private function __construct(){
require_once PLUGINPATH.'rb.php';
$this->connection = R::setup("mysql:host=192.168.1.1;dbname=myDatabase",'username','password');
}
public static function get() {
static $db = null;
if ( $db === null )
$db = new database();
return $db;
}
public function connection() {
return $this->connection;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
如果我?month=$04
在网址和回声中传递$date
我继续接收1月而不是它应该是什么(4月).如果我回应$month
我得到04这是正确的.这是我一直在使用的代码:
if (isset($_GET['month']) && $_GET['month']!='') {
$month = $_GET['month'];
$date = date('F', $month);
}
echo $date;
Run Code Online (Sandbox Code Playgroud)
对于我的生活,我无法弄清楚为什么它输出不正确.任何帮助非常感谢.
此方法搜索搜索关键字并解析mysql查询,并重写where表达式以包含LIKE%keyword%.
它运作良好,但我不知道它的好或坏做法是否有这么多循环的方法...
private function build_where($query_array, $options)
{
//add WHERE starting point
$where = '';
if(!empty($query_array['WHERE']))
{
//build where array
$where_array = $query_array['WHERE'];
//start the where
$where .= 'WHERE ';
//get columns array
$columns_array = $this->build_columns_array($query_array);
//if there is a search string
if(!empty($options['sSearch']))
{
//check for enabled columns
$i = 0;
$columns_length = count($columns_array);
for($i; $i < intval($columns_length); $i++)
{
//create the options boolean array
$searchable_columns['bSearchable_'.$i] = $options['bSearchable_'.$i];
}
//loop through searchable_columns for true values
foreach($searchable_columns as $searchable_column_key => $searchable_column_val)
{ …
Run Code Online (Sandbox Code Playgroud) 我正在寻找一些可以对PHP脚本进行基本测试的工具.
这次我对复杂的测试解决方案不感兴趣,需要为每一段代码编写测试.我对功能测试既不感兴趣.
我期望这个工具做的是指出可能导致无效使用的代码片段:
例如,无效的使用将使用NULL作为对象或数组,或者将boolean作为需要为数组或对象(或NULL)的arguent传递.
此外,它还可能会检查是否使用了"良好做法",即它可能会警告使用它是不安全的if ( $condition ) $doSomething;
,如果它找到类似的东西.
为了说明该工具应该检测为"不安全"的代码片段,这里有几个例子.
在这种情况下,$filter
可能是NULL
,但它用作数组:
<?php
function getList(array $filter = null) {
$sql = '...';
// ...
foreach ( $filter as $field => $value ) {
// ...
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,$res
可能是false
,但它被用作resource
.
<?php
$res = mysql_query('...');
while ( $row = mysql_fetch_assoc($res) ) {
// ...
}
mysql_free_result($res);
Run Code Online (Sandbox Code Playgroud)
这里是相应的"安全"代码片段:
// $filter is used only if it's non-empty; as list …
Run Code Online (Sandbox Code Playgroud) 你好。
由于我使用的是共享托管包,并且无法使用 PECL Memcache,因此我希望能提供一些有关使用我自己的小型缓存系统或使用 PEAR Cache_Lite 系统之间的疑虑的提示。
所以这是我的功能:
<?php
//this one create a simple .txt file named by unique query_key string generated width e.g $file_name=md5("SELECT * FROM table"); content of that file is serialized value of mysql return
function write($query_key,$result)
{
global $config;
$new_file_name=md5($query_key);
$result_to_write=serialize($result);
if($handle=opendir($config['cache_dir']))
{
$f=fopen($config['cache_dir'].$new_file_name.'.txt','w+');
fwrite($f,$result_to_write);
fclose($f);
closedir($handle);
}
}
// this one reads content of file (serialized mysql return of unique query_key) if timeout hes not expired, and if it is it return false
function read($query_key,$timeout) …
Run Code Online (Sandbox Code Playgroud) 我是一位经验丰富的开发人员,通常使用 C# 和 Python 进行开发,做过大量元编程。老实说:我不喜欢 PHP,但最近的版本看起来至少比旧版本更有希望。我最近必须在 PHP 项目中工作,所以我必须提高我在这方面的技能。
我正在寻找资源以尽快了解 PHP 语言的详细信息。我当然能够阅读文档,而且我不是在寻找一些印刷的 API 文档。
我想了解这些语言背后的设计思想以及什么样的元编程是可能的。
任何提示?
我需要拆分一个包含逗号的字符串.我已经找到像(str_getcsv
)这样的字符串的东西:
'A', 'B with a comma, eh', 'C'
Run Code Online (Sandbox Code Playgroud)
但我的字符串是这样的,例如没有值的封闭字符:
A, B (one, two), C
Run Code Online (Sandbox Code Playgroud)
我需要爆炸并获得:
array(3) {
[0]=>
string(1) "A"
[1]=>
string(12) "B (one, two)"
[2]=>
string(1) "C"
}
Run Code Online (Sandbox Code Playgroud)
我想使用不在括号内的逗号分割字符串,因为这是我爆炸失败时唯一的情况.
我需要实现基于角色的消息发送功能。
我必须适应有关哪些角色可以使用哪些消息发送功能的更改,例如,我需要能够将角色 B 更改为还包括短信。任何角色都可能需要任何消息发送功能。
我考虑过有一个接口 IMessageChannel 和一个方法 SendMessage。然后是实现该接口的三个类,例如电子邮件、短信和 Twitter。我正在考虑使用策略模式和工厂?这样对吗?
我应该考虑什么设计模式来实现这个?