php - @ operator

Development/PHP 2015. 6. 11. 17:19


Error Control Operators ¶

PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.

If you have set a custom error handler function with set_error_handler() then it will still get called, but this custom error handler can (and should) call error_reporting() which will return 0 when the call that triggered the error was preceded by an @.

If the track_errors feature is enabled, any error message generated by the expression will be saved in the variable $php_errormsg. This variable will be overwritten on each error, so check early if you want to use it.

<?php
/* Intentional file error */
$my_file = @file ('non_existent_file') or
    die (
"Failed opening file: error was '$php_errormsg'");

// this works for any expression, not just functions:
$value = @$cache[$key];
// will not issue a notice if the index $key doesn't exist.

?>

NoteThe @-operator works only on expressions. A simple rule of thumb is: if you can take the value of something, you can prepend the @ operator to it. For instance, you can prepend it to variables, function and include calls, constants, and so forth. You cannot prepend it to function or class definitions, or conditional structures such as if and foreach, and so forth.

See also error_reporting() and the manual section for Error Handling and Logging functions.

Warning

Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.



source - http://php.net/manual/en/language.operators.errorcontrol.php







You also can use the @ operator to mute errors occurring in the constructor, e.g. @new.



source - http://php.net/manual/kr/oop4.constructor.php

'Development > PHP' 카테고리의 다른 글

php - stdClass  (0) 2015.06.10
php - eclipse debugging  (0) 2015.06.04
php - eclipse zend debugger plugin install  (0) 2015.06.03
php - array  (0) 2013.06.19
php - foreach 문  (0) 2013.06.19
Posted by linuxism
,

php - stdClass

Development/PHP 2015. 6. 10. 17:21


Object Initialization ¶

To create a new object, use the new statement to instantiate a class:

<?php
class foo
{
    function 
do_foo()
    {
        echo 
"Doing foo."
    }
}

$bar = new foo;
$bar->do_foo();
?>

For a full discussion, see the Classes and Objects chapter.

Converting to object ¶

If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClassbuilt-in class is created. If the value was NULL, the new instance will be empty. An array converts to an object with properties named by keys and corresponding values, with the exception of numeric keys which will be inaccessible unless iterated.

<?php
$obj 
= (object) array('1' => 'foo');
var_dump(isset($obj->{'1'})); // outputs 'bool(false)'
var_dump(key($obj)); // outputs 'int(1)'
?>

For any other value, a member variable named scalar will contain the value.

<?php
$obj 
= (object) 'ciao';
echo 
$obj->scalar;  // outputs 'ciao'
?>



source - http://php.net/manual/en/language.types.object.php








Have you ever seen a scenario where you’re accessing data as an object, such as when you’re using WordPress’s database interface or when you’re parsing XML files with SimpleXML? You have something like echo $result->name.

Have you ever wondered how that was done? Using PHP’s stdClass feature you can create an object, and assign data to it, without having to formally define a class.

Suppose you wanted to quickly create a new object to hold some data about a book. You would do something like this:

1
2
3
4
5
$book = new stdClass;
$book->title = "Harry Potter and the Prisoner of Azkaban";
$book->author = "J. K. Rowling";
$book->publisher = "Arthur A. Levine Books";
$book->amazon_link = "http://www.amazon.com/dp/0439136369/";

You can then access the data by calling $book->title and so on.

Or what if you wanted to take an array and turn it into an object? You can do that by casting the variable.

1
2
3
4
5
6
7
8
$array = array(
"title" => "Harry Potter and the Prisoner of Azkaban",
"author" => "J. K. Rowling",
"publisher" => "Arthur A. Levine Books",
);
 
$books = (object) $array;

This should produce the same result as the previous code snippet. You could also go the other direction, casting an object into an array by using $array = (array) $object.

Objects are really useful when you’re working with a large data structure, as you can have an object with nested sub-arrays in it, as SimpleXML tends to return. You get most of the data as properties of the result set object, and the posts in an RSS feed are assigned to array entries, which then have further objects inside them.



source - http://www.webmaster-source.com/2009/08/20/php-stdclass-storing-data-object-instead-array/



'Development > PHP' 카테고리의 다른 글

php - @ operator  (0) 2015.06.11
php - eclipse debugging  (0) 2015.06.04
php - eclipse zend debugger plugin install  (0) 2015.06.03
php - array  (0) 2013.06.19
php - foreach 문  (0) 2013.06.19
Posted by linuxism
,


The general query log is a log of every SQL query received from a client, as well as each client connect and disconnect.

Activating the General Query Log

The general query log is disabled by default. Since it's a record of every query received by the server, it can grow large quite quickly. If you only want a record of queries that change data, rather use thebinary log instead.

To activate the general query log, set the general_log system variable to 1. The log_output server status variable determines how the output will be written, and can also disable it (see Writing logs into tables for details). If written to file, the name of the file is determined by the --general_log_file=file_name option, by default host_name.log, while the directory will be the data directory unless an absolute path is specified.

Unlike the binary log, the query log records queries in the same order that the server receives them, and not necessarily the same order that they are executed.

Example of how to log to file

By adding this to your my.cnf file all queries will be logged to the file queries.log in the datadir directory.

[[mysqld]]
general-log
general-log-file=queries.log
log-output=file

Another option is to use the log-basename (see mysqld replication options) option which ensures that all your log files (general log, replication logs etc) start with a common unique prefix. The following example will create a system.log log file.

[[mysqld]]
general-log
log-output=file
log-basename=system



source - https://mariadb.com/kb/en/mariadb/general-query-log/


'DB > MariaDB' 카테고리의 다른 글

mariadb - install  (0) 2014.07.20
Posted by linuxism
,