PHP Equivalent of Friend or Internal

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

369 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

Include a PHP INI File in Another PHP INI File

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

489 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

1K+ 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

Difference Between fgets and fread in PHP

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

2K+ 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

Read Single File Inside a ZIP Archive with PHP

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

1K+ 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 vs PHP Mess Detector vs PHP Depend

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

215 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

PHP String Comparison: 0 vs String

AmitDiwan
Updated on 07-Apr-2020 12:56:04

241 Views

The syntax ‘$string{0} ‘ has been deprecated beginning from PHP version 6. Hence, it is strongly suggested to use $string[0].In short, accessing characters using the flower braces {} has been deprecated. Hence the square brackets should be used [] −Example Live Demo$string = 'medium'; echo $string{0}; echo $string[0];OutputThis will produce the following output −mm

Sort Output in PowerShell

Chirag Nagrekar
Updated on 07-Apr-2020 12:25:24

11K+ Views

To sort the output in the PowerShell you need to use Sort-Object Pipeline cmdlet. In the below example, we will retrieve the output from the Get-Process command and we will sort the, according to memory and CPU usage.ExampleGet-Process | Sort-Object WorkingSet | Select -First 10OutputHandles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName -------  ------    -----      -----     ------     --  -- -----------       0       0       60          8                 0 ... Read More

What are Automatic Modules in Java 9

raja
Updated on 07-Apr-2020 12:22:23

417 Views

An automatic module is a jar that we put on the modulepath. There is a number of pre-existing libraries that can be used in our applications, and many of these are not yet modularized. To facilitate migration, we can add any library’s jar file to an application’s module path, then use packages in that jar file. It can implicitly become an automatic module and can be specified in the module declaration’s requires directive. The jar’s filename becomes its module name that must be a valid Java identifier that can be used in "requires" directive.An automatic module:Implicitly exports all package types, so ... Read More

Use Measure-Object in PowerShell

Chirag Nagrekar
Updated on 07-Apr-2020 12:21:33

2K+ Views

Measure-Object in PowerShell is used to measure the property of the command. There are various measurement parameters are available. For example, Average, Count, sum, maximum, minimum and more.ExampleGet-Process | Measure-ObjectOutputPS C:\WINDOWS\system32> Get-Process | Measure-Object Count       : 278 Average     : Sum         : Maximum     : Minimum     : Property    :Here, in the above output, there are total 278 processes are running. If you want to check the maximum memory usage then you can use WorkingSet property with − Maximum Parameter.Get-Process | Measure-Object -Property WorkingSet -MaximumOutputPS C:\WINDOWS\system32> Get-Process | Measure-Object ... Read More

Advertisements