本文整理汇总了PHP中singleton类的典型用法代码示例。如果您正苦于以下问题:PHP singleton类的具体用法?PHP singleton怎么用?PHP singleton使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了singleton类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: singleton
/**
* This function returns a instance (and creates a new one
* if there isnt already one) of a class.
*
* @author Johannes Klose <exe@calitrix.de>
* @param string $class Class from where an instance should be returned.
* @return object Instance of $class
**/
function &singleton($class)
{
static $singleton;
if (!is_object($singleton)) {
$singleton = new singleton();
}
return $singleton->instance($class);
}
示例2: getInstance
public static function getInstance(&$sql, $location, $var, $type)
{
// 确定类型
switch ($type) {
default:
// 默认使用字符串类型
// 默认使用字符串类型
case 'STRING':
$var = addslashes($var);
// 转义
$var = "'" . $var . "'";
// 加上单引号.sql语句中字符串插入必须加单引号
break;
case 'INTEGER':
case 'INT':
$var = (int) $var;
// 强制转换成int
// 还可以增加更多的类型...
}
$pos = 0;
// 判断该类是否是第一次被实例化
if (self::$instance == NULL) {
self::$instance = new singleton();
for ($i = 1; $i <= $location; $i++) {
$pos = strpos($sql, '?', $pos + 1);
}
} else {
for ($i = 1; $i <= $location - 1; $i++) {
$pos = strpos($sql, '?', $pos + 1);
}
}
return $sql = substr($sql, 0, $pos) . $var . substr($sql, $pos + 1);
}
示例3: get_instance
public static function get_instance($text)
{
if (!isset(singleton::$singleton)) {
singleton::$singleton = new singleton($text);
}
return singleton::$singleton;
}
示例4: getInstance
public static function getInstance()
{
if (self::$instance == null) {
self::$instance = new singleton();
}
return self::$instance;
}
示例5: update_check
public function update_check()
{
$config =& singleton::get(__NAMESPACE__ . '\\config');
$apptrack =& singleton::get(__NAMESPACE__ . '\\apptrack');
$send_data['application_id'] = 1;
$send_data['version'] = $config->get('program_version');
$data = $apptrack->send($send_data);
if (!empty($data)) {
$config->set('last_update_response', $data);
return true;
} else {
$log =& singleton::get(__NAMESPACE__ . '\\log');
$log_array['event_severity'] = 'warning';
$log_array['event_number'] = E_USER_WARNING;
$log_array['event_description'] = 'Unable to contact update server.';
$log_array['event_file'] = __FILE__;
$log_array['event_file_line'] = __LINE__;
$log_array['event_type'] = 'update_check';
$log_array['event_source'] = 'cron';
$log_array['event_version'] = '1';
$log_array['log_backtrace'] = false;
$log->add($log_array);
return false;
}
}
示例6: get
public function get($name)
{
$error =& singleton::get(__NAMESPACE__ . '\\error');
if (isset($this->language_array[$name])) {
//return '試験';
//return '試験' . $this->language_array[$name];
return $this->language_array[$name];
} else {
//$error->create(array('type' => 'language_item_missing', 'message' => 'Unable to find language item "'.$name.'".'));
return $name;
}
}
示例7: optimise_tables
public function optimise_tables()
{
global $db;
$tables =& singleton::get(__NAMESPACE__ . '\\tables');
$log =& singleton::get(__NAMESPACE__ . '\\log');
$log_array['event_severity'] = 'notice';
$log_array['event_number'] = E_USER_NOTICE;
$log_array['event_description'] = 'Optimising Tables';
$log_array['event_file'] = __FILE__;
$log_array['event_file_line'] = __LINE__;
$log_array['event_type'] = 'optimise_tables';
$log_array['event_source'] = 'db_maintenance';
$log_array['event_version'] = '1';
$log_array['log_backtrace'] = false;
$log->add($log_array);
$optimise_tables = '';
foreach ($tables->get() as $value => $index) {
$optimise_tables .= $index . ',';
}
$optimise_tables = substr($optimise_tables, 0, strlen($optimise_tables) - 1);
$query = 'OPTIMIZE TABLE ' . $optimise_tables;
foreach ($db->query($query, database::FETCH_ASSOC) as $row) {
if ($row['Msg_type'] == 'error') {
$number = E_USER_WARNING;
$type = 'warning';
} else {
$number = E_USER_NOTICE;
$type = 'notice';
}
$log_array['event_severity'] = $type;
$log_array['event_number'] = $number;
$log_array['event_description'] = 'Table "' . $row['Table'] . '"<br />Message "' . $row['Msg_text'] . '"';
$log_array['event_file'] = __FILE__;
$log_array['event_file_line'] = __LINE__;
$log_array['event_type'] = 'optimise_tables';
$log_array['event_source'] = 'db_maintenance';
$log_array['event_version'] = '1';
$log_array['log_backtrace'] = false;
$log->add($log_array);
}
$log_array['event_severity'] = 'notice';
$log_array['event_number'] = E_USER_NOTICE;
$log_array['event_description'] = 'Optimising Tables Complete';
$log_array['event_file'] = __FILE__;
$log_array['event_file_line'] = __LINE__;
$log_array['event_type'] = 'optimise_tables';
$log_array['event_source'] = 'db_maintenance';
$log_array['event_version'] = '1';
$log_array['log_backtrace'] = false;
$log->add($log_array);
}
示例8: send
public function send($array)
{
$log =& singleton::get(__NAMESPACE__ . '\\log');
$array['token'] = $this->app_key;
/*
Limit to 512 chars as per pushover limit.
*/
$remove = 0;
if (isset($array['title'])) {
$remove = (int) strlen($array['title']);
}
if (isset($array['message'])) {
$array['message'] = substr($array['message'], 0, 512 - $remove);
}
$options = array('http' => array('user_agent' => user_agent(), 'timeout' => 5, 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query($array)));
$context = stream_context_create($options);
$result = @file_get_contents($this->api_url, false, $context);
if ($result) {
$return_data = json_decode($result, true);
if ($return_data['status'] == 1) {
return true;
} else {
$larray['event_severity'] = 'error';
$larray['event_number'] = E_USER_ERROR;
$larray['event_description'] = 'Unable to send pushover message.';
$larray['event_file'] = __FILE__;
$larray['event_file_line'] = __LINE__;
$larray['event_type'] = 'send';
$larray['event_source'] = 'pushover';
$larray['event_version'] = '1';
$larray['log_backtrace'] = true;
$log->add($larray);
}
} else {
$larray['event_severity'] = 'error';
$larray['event_number'] = E_USER_ERROR;
$larray['event_description'] = 'Unable to send pushover message.';
$larray['event_file'] = __FILE__;
$larray['event_file_line'] = __LINE__;
$larray['event_type'] = 'send';
$larray['event_source'] = 'pushover';
$larray['event_version'] = '1';
$larray['log_backtrace'] = true;
$log->add($larray);
}
return false;
}
示例9: validateAddPortfolio
/**
* validateAddPortfolio
*
* Validate new portfolio
* @param($_POST)
* @return(boolean)
*/
public function validateAddPortfolio()
{
# get the modal
$oPortfolio = singleton::getInstance('Portfolio');
# the slug
$page_slug = $_POST['page_slug'];
# create empty error
$aErrors = array();
if (strlen($_POST['portfolio_title']) >= 30) {
$aErrors['title'] = true;
}
if (strlen($_POST['portfolio_description']) >= 1000) {
$aErrors['description'] = true;
}
if (strlen($_POST['portfolio_github_link']) >= 100) {
$aErrors['github'] = true;
}
if (strlen($_POST['portfolio_website_link']) >= 100) {
$aErrors['portfolio_website_link'] = true;
}
if (strlen($_POST['portfolio_meta_title']) >= 100) {
$aErrors['portfolio_meta_title'] = true;
}
if (strlen($_POST['portfolio_meta_keyword']) >= 1000) {
$aErrors['portfolio_meta_title'] = true;
}
if (strlen($_POST['portfolio_meta_desc']) >= 2) {
$aErrors['portfolio_meta_desc'] = true;
}
if (strlen($page_slug) >= 25) {
$aErrors['page_slug_too_long'] = true;
}
# check regex
if (!preg_match('/^[a-z0-9-]+$/', $page_slug)) {
$aErrors['not_a_valid_regex'] = true;
}
# check if availble
if (!$oPortfolio->checkSlugAvailable($page_slug)) {
$aErrors['slug_not_available'] = true;
}
if ($aErrors) {
return var_dump($aErrors);
} else {
return true;
}
}
示例10: can_view
private function can_view($array)
{
$auth =& singleton::get(__NAMESPACE__ . '\\auth');
$tickets =& singleton::get(__NAMESPACE__ . '\\tickets');
$user_level = (int) $auth->get('user_level');
switch ($user_level) {
case 6:
//admin
break;
case 5:
//moderator
$get_array['department_or_assigned_or_user_id'] = $auth->get('id');
break;
case 4:
//staff member
$get_array['department_or_assigned_or_user_id'] = $auth->get('id');
break;
case 3:
//user
$get_array['assigned_or_user_id'] = $auth->get('id');
break;
case 2:
//global moderator
break;
default:
//sub
$get_array['user_id'] = $auth->get('id');
break;
}
$get_array['count'] = true;
$get_array['id'] = (int) $array['id'];
$result = $tickets->get($get_array);
if (!empty($result) && $result[0]['count'] != 0) {
return true;
} else {
return false;
}
}
示例11: display
function display()
{
$config =& singleton::get(__NAMESPACE__ . '\\config');
$language =& singleton::get(__NAMESPACE__ . '\\language');
header("Content-type: image/png");
if (isset($this->text) && !empty($this->text)) {
$capture_text = $this->text;
} else {
$capture_text = $language->get('Error');
}
$string = strtoupper($capture_text);
$r = rand(0, 150);
$g = rand(0, 150);
$b = rand(0, 150);
$im = imagecreatefrompng(THEMES . '/' . CURRENT_THEME . '/images/captcha_background.png');
$colour = imagecolorallocate($im, $r, $g, $b);
$size = rand(20, 25);
$angle = rand(0, 3);
$left = rand(5, 17);
$bottomleft = 38;
imagettftext($im, $size, $angle, $left, $bottomleft, $colour, SYSTEM . "/fonts/delicious.otf", $string);
imagepng($im);
imagedestroy($im);
}
示例12: html_output
/**
* Returns an HTML string while stripping out bad HTML
*
* @param string $string The HTML to make safe
* @return string The safe HTML
*/
function html_output($string)
{
$purifier =& singleton::get('HTMLPurifier');
return $purifier->purify($string);
}
示例13: _vimeography_subscribe_to_trigger
/**
* When the user enters the source location when creating a new gallery.
*
* Won't work publically yet, because the user needs to be authenticated to subscribe to push notifications.
* Also, does not currently work with albums.
*
* @return [type] [description]
*/
private function _vimeography_subscribe_to_trigger($resource, $gallery_id)
{
$callback = network_site_url() . '/vimeography/' . $gallery_id . '/refresh/';
$response = $this->_vimeo->request('/triggers', array('actions' => 'added, removed', 'callback' => $callback, 'resource_uri' => $resource . '/videos'), 'POST');
echo '<pre>';
var_dump($response);
echo '</pre>';
die;
switch ($response['status']) {
case 201:
//successful
return TRUE;
break;
case 403:
if ($this->_token === FALSE) {
// Trigger unsuccessful, rely on 304 headers.
break;
// This line will only work when the Vimeo API supports triggers without being authenticated
// Though, the user could technically be subscribing to a collection that isn't actually supported in PRO, either.
// So be specific in which sources are currently supported.
//throw new Vimeography_Exception('Vimeography PRO allows you to show videos from all of your users, channels, albums, & groups.');
} else {
throw new Vimeography_Exception(__("Looks like you don't have the permission to subscribe to this collection.", 'vimeography'));
}
break;
case 405:
case 500:
// Unsupported container uri
throw new Vimeography_Exception(__('The resource that was entered is currently unsupported.', 'vimeography'));
break;
default:
throw new Vimeography_Exception(serialize($response));
break;
}
}
示例14: getInstance
public static function getInstance()
{
if (NULL === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
示例15: getInstance
public static function getInstance()
{
if (!self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}