什么是"::" PHP中的符号用于? [英] what is the "::" notation in php used for?
问题描述
我正在查看一些php代码,并且看到了"::"符号,表示我不知道这意味着什么...还有&是什么.在通话的前面
I am looking through some php code and I see this "::" notation that i have no idea what it means...also what the the & in the front of the call
$mainframe =& JFactory::getApplication('site');
$sql="SELECT rt.member_id ,rt.commission,rt.sales,kt.store_id,kt.user_id FROM jos_report
rt JOIN jos_kingdom_tickets kt WHERE rt.member_id=kt.ticket_id";
$db =& JFactory::getDBO();
预先感谢
推荐答案
::
,范围解析运算符,用于引用类的静态成员和常量.它还用于引用超类的构造函数.以下代码说明了范围解析运算符的几种不同用法:
::
, the scope resolution operator, is used for referencing static members and constants of a class. It is also used to reference a superclass's constructor. Here is some code illustrating several different uses of the scope resolution operator:
<?php
class A {
const BAR = 1;
public static $foo = 2;
private $silly;
public function __construct() {
$this->silly = self::BAR;
}
}
class B extends A {
public function __construct() {
parent::__construct();
}
public static function getStuff() {
return 'this is tiring stuff.';
}
}
echo A::BAR;
echo A::$foo;
echo B::getStuff();
?>
一些琐事:范围解析运算符也称为"paamayim nekudotayim",意思是希伯来语中的两个点两次".
A little trivia: The scope resolution operator is also called "paamayim nekudotayim", which means "two dots twice" in hebrew.
&
不会做任何有用的事情,应将其删除.在php 4中,这曾经是必须的,以确保未使用返回对象的副本.在php 5中,除非调用 clone ,否则不会创建对象副本.因此,不需要&
.仍然有一种情况,&
在php 5中仍然有用:在遍历数组的元素并修改值时,必须使用&
运算符来影响数组的元素.
&
in the context of your example isn't doing anything useful if you are using php 5 or greater and should be removed. In php 4, this used to be necessary in order to make sure a copy of the returned object wasn't being used. In php 5 object copies are not created unless clone is called. And so &
is not needed. There is still one case where &
is still useful in php 5: When you are iterating over the elements of an array and modifying the values, you must use the &
operator to affect the elements of the array.
这篇关于什么是"::" PHP中的符号用于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!