我知道这个问题听起来很模糊,所以我会用一个例子说清楚:
$var = 'bar';
$bar = new {$var}Class('var for __construct()'); //$bar = new barClass('var for __construct()');
Run Code Online (Sandbox Code Playgroud)
这就是我想要做的.你会怎么做?我当然可以使用这样的eval():
$var = 'bar';
eval('$bar = new '.$var.'Class(\'var for __construct()\');');
Run Code Online (Sandbox Code Playgroud)
但我宁愿远离eval().没有eval()有没有办法做到这一点?
在jQuery中,您可以将相对于父级的顶部位置作为数字,但如果设置了,则无法将css顶部值作为数字px
.
说我有以下内容:
#elem{
position:relative;
top:10px;
}
Run Code Online (Sandbox Code Playgroud)
<div>
Bla text bla this takes op vertical space....
<div id='elem'>bla</div>
</div>
Run Code Online (Sandbox Code Playgroud)
$('#elem').position().top; //Returns the number (10+(the vertical space took by the text))
$('#elem').css('top'); //Returns the string '10px'
Run Code Online (Sandbox Code Playgroud)
但我希望将css top属性作为数字10
.
如何实现这一目标?
类可以扩展PHP中的接口和另一个类吗?
基本上我想这样做:
interface databaseInterface{
public function query($q);
public function escape($s);
//more methods
}
class database{ //extends both mysqli and implements databaseInterface
//etc.
}
Run Code Online (Sandbox Code Playgroud)
如何做到这一点,只需做:
class database implements databaseInterface extends mysqli{
Run Code Online (Sandbox Code Playgroud)
导致致命错误:
Parse error: syntax error, unexpected T_EXTENDS, expecting '{' in *file* on line *line*
在javascript中,您可以轻松创建对象和数组,如下所示:
var aObject = { foo:'bla', bar:2 };
var anArray = ['foo', 'bar', 2];
Run Code Online (Sandbox Code Playgroud)
PHP中类似的东西可能吗?
我知道你可以使用数组函数轻松创建一个数组,这几乎不是javascript语法的工作,但有没有类似的语法来创建对象?或者我应该只使用关联数组?
$anArray = array('foo', 'bar', 2);
$anObjectLikeAssociativeArray = array('foo'=>'bla',
'bar'=>2);
Run Code Online (Sandbox Code Playgroud)
总结一下:
PHP是否有像对象创建这样的javascript或我应该只使用关联数组?
我不想使用以下HTML,CSS和javascript动态地更改div的背景颜色.HTML:
<div id="menu">
<div class="menuItem"><a href=#>Bla</a></div>
<div class="menuItem"><a href=#>Bla</a></div>
<div class="menuItem"><a href=#>Bla</a></div>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS:
.menuItem{
display:inline;
height:30px;
width:100px;
background-color:#000;
Run Code Online (Sandbox Code Playgroud)
使用Javascript:
$('.menuItem').hover( function(){
$(this).css('background-color', '#F00');
},
function(){
$(this).css('background-color', '#000');
});
Run Code Online (Sandbox Code Playgroud)
编辑:我忘了说我有理由不想使用css方式.
我确实忘了检查DOM是否已加载.
我读了一些(我想在编码错误),将字符串加在一起好像它们是数字是不好的做法,因为像数字一样,字符串不能改变.因此,将它们一起添加会创建一个新字符串.所以,我想知道,在关注性能时,将两个字符串组合在一起的最佳方法是什么?
这四种中哪一种更好,还是有另一种方式更好?
//Note that normally at least one of these two strings is variable
$str1 = 'Hello ';
$str2 = 'World!';
$output1 = $str1.$str2; //This is said to be bad
$str1 = 'Hello ';
$output2 = $str1.'World!'; //Also bad
$str1 = 'Hello';
$str2 = 'World!';
$output3 = sprintf('%s %s', $str1, $str2); //Good?
//This last one is probaply more common as:
//$output = sprintf('%s %s', 'Hello', 'World!');
$str1 = 'Hello ';
$str2 = '{a}World!';
$output4 = str_replace('{a}', $str1, $str2);
Run Code Online (Sandbox Code Playgroud)
它甚至重要吗?
好吧,我用PHP和JavaScript开发Web应用程序,很多时候在Stack Overflow上我已经看到单词测试通过了,但是在网站上我没有找到一个令人满意的答案,单元测试到底是什么.
那么什么是单元测试?作为PHP和JavaScript程序员,我应该关心,还是仅仅针对"真正的"编程语言?
我正在寻找jQuery的工具提示插件,它将允许以下类型的行为.
<a href="somewhere.html">
<span>
<img src="someimage.jpg" style="display: none;" />
Here is the tooltip content.
</span>
Here is the link to somewhere.
</a>
Run Code Online (Sandbox Code Playgroud)
我希望的行为是将鼠标悬停在"这里是某个地方的链接"上,并弹出一个工具提示,显示包含"someimage.jpg"和"这是工具提示内容"的范围内容.
我希望工具提示跟踪鼠标在链接上的移动以及工具提示的外观(背景颜色,不透明度,边框颜色等)是可配置的.
我发现的两个最流行的工具提示,"clueTip"和JörnZaefferer的"工具提示"似乎不符合要求,除非我遗漏了一些东西.
最终,链接和图像将动态生成.
如何使用ModRewrite检查缓存文件是否存在,如果存在,则重写缓存文件,否则重写为动态文件.
例如,我有以下文件夹结构:
pages.php cache/ pages/ 1.html 2.html textToo.html etc.
你如何为此设置RewriteRules,所以请求可以像这样发送:
example.com/pages/1
如果缓存文件存在,则重写缓存文件,如果缓存文件不存在,则重写为pages.php?p = 1
它应该是这样的:(请注意,这不起作用,否则我不会问这个)
RewriteRule ^pages/([^/\.]+) cache/pages/$1.html [NC,QSA] RewriteCond %{REQUEST_FILENAME} -f [NC,OR] RewriteCond %{REQUEST_FILENAME} -d [NC] RewriteRule cache/pages/([^/\.]+).html pages.php?p=$1 [NC,QSA,L]
我可以使用PHP来粗略地做这个,但我认为必须使用mod_rewrite.
是否可以读取Apache中.htaccess文件中的php $ _SESSION数组中的数据?所以说我有以下内容:
$_SESSION['foo'] = 'bar';
Run Code Online (Sandbox Code Playgroud)
我可以在.htaccess做类似的事情:
RewriteRule bla.png folder/{the php session var foo}/file.png
Run Code Online (Sandbox Code Playgroud)
那可能吗?
我已经有了一个可行的解决方法,但如果可行的话,它会更好.
php ×6
javascript ×3
jquery ×3
apache ×2
css ×2
.htaccess ×1
caching ×1
class ×1
eval ×1
html ×1
mod-rewrite ×1
object ×1
performance ×1
session ×1
string ×1
tooltip ×1
unit-testing ×1
variables ×1