Found 1046 Articles for PHP

Getting size in memory of an object in PHP?

AmitDiwan
Updated on 07-Apr-2020 13:17:10

1K+ Views

The memory_get_usage() function can be caked before and after allocating memory to the class created.class MyBigClass {    var $allocatedSize;    var $allMyOtherStuff; } function AllocateMyBigClass() {    $before = memory_get_usage();    $ret = new MyBigClass;    $after = memory_get_usage();    $ret->allocatedSize = ($after - $before);    return $ret; }Output will be the memory of object with respect to the environment setup.

PHP Call methods of objects in array using array_map?

AmitDiwan
Updated on 07-Apr-2020 13:15:01

985 Views

In PHP version 5.3, methods of objects in array can be called using the below code −$props = array_map(function($obj){ return $obj->getProp(); }, $objs);This will be slower than a ‘for’ loop since it invokes one function for every element −function map($obj) {    return $obj->getProperty(); } $props = array_map('map', $objs);Alternatively, for PHP versions before 5.3, the below code can be used −function map($obj) {    return $obj-> getProperty (); } $props = array_map('map', $objs); }The getProperty function will be called on all the objects and the specific property is displayed. Alternative −function encode_data($val){    if(is_array($val)){       return $val = ... Read More

Is it possible to have a HTML SELECT/OPTION value as NULL using PHP?

AmitDiwan
Updated on 07-Apr-2020 13:12:47

1K+ Views

The short answer is no. POST/GET values are never null. The best they can be is an empty string, which can then be converted to null/'NULL' −Example Live Demoif ($_POST['value'] === '') {    $_POST['value'] = null; } echo'Null assigned';OutputThis will produce the following output −Null assigned

PHP equivalent of friend or internal

AmitDiwan
Updated on 07-Apr-2020 13:09:55

224 Views

PHP doesn't support friend-like declarations. It can be simulated in PHP5 using the __get and __set methods and by inspecting a backtrace for the allowed friend classes. But this type of coding practice is considered to be clumsy −class sample_friend {    private $__friends = array('My_Friend', 'Other_Friend');    public function __get($key)    {       $trace = debug_backtrace();       if(isset($trace[1]['class']) && in_array($trace[1]['class'], $this->__friends)) {          return $this->$key;       }       // __get() code goes here       trigger_error('Cannot access private property ' . __CLASS__ . '::$' . $key, E_USER_ERROR); ... Read More

How do I include a php.ini file in another php.ini file?

AmitDiwan
Updated on 07-Apr-2020 13:05:48

326 Views

The .ini files from the main php,ini file can’t be included. Instead, while compiling PHP, the line--with-config-file-scan-dir=PATH                                                can be added.The ‘PATH’ in the above line refers to the location where the configuration file is to be scanned.During compile time, PHP will look for each and every .ini file in that specific directory, apart from searching in the normal php.ini file.

Tracking Memory Usage in PHP

AmitDiwan
Updated on 07-Apr-2020 13:03:39

904 Views

The memory_get_usage function can be used to track the memory usage. The ‘malloc’ function is not used for every block required, instead a big chunk of system memory is allocated and the environment variable is changed and managed internally.The two different types of memory usages are −The memory required by the engine from OS (the real usage)The amount of memory that was actually used by the application (internal usage)The above mentioned memory usage can be tracked using memory_get_usage(). This function returns both real and actual memory used depending on our requirement.For example: if we are looking at specific code snippets, ... Read More

fgets() and fread() - What is the difference in PHP?

AmitDiwan
Updated on 07-Apr-2020 13:02:16

1K+ Views

The ‘fgets’ function reads a line and stops when it encounters a newline −The above code opens a text file named ‘test’ in the read mode and reads the contents of the file until a newline character is encountered beginning from the starting byte. The file is then closed.The ‘fread’ function reads raw data and stops after a specific number of bytes or default bytes. This doesn’t depend on whether a newline was encountered or not −The above code opens a text file named ‘test’ in the read mode and reads 10 bytes after the starting byte. The file is ... Read More

How to read a single file inside a zip archive with PHP

AmitDiwan
Updated on 07-Apr-2020 13:00:34

802 Views

To read a single fine inside z zip archive, the code is as follows −$handle = fopen('zip://test.zip#test.txt', 'r'); $result = ''; while (!feof($handle)) {    $result .= fread($handle, 8192); } fclose($handle); echo $result;The output will be the contents of the zip file.

PHP_CodeSniffer, PHPMD or PHP Depend

AmitDiwan
Updated on 07-Apr-2020 12:59:01

112 Views

pdependThe function pdepend is used to generate a large set of software metrics from a given code base. The generated values can be used to measure the quality of a software project. They help in identifying the parts of an application where refactoring is required.phpmdThe phpmd scans the PHP source code and searches for potential problems that could be possible bugs, not-so-optimal code, or overcomplicated expressions.phpcsThe phpcs function tokenises the PHP, JavaScript and CSS files and figures out issues/violations in a set of pre-defined coding standards. It ensures that the code remains consistent and neat. It also helps in preventing ... Read More

Passing static methods as arguments in PHP 

AmitDiwan
Updated on 07-Apr-2020 14:20:40

581 Views

The same syntax used by is_callable and call_user_func can be used to pass static methods as arguments in PHP.To pass the static method, the below example can be used −Example Live Demo OutputThis will produce the following output −bool(true) my_func bool(true)

Advertisements