Title: Clean PHP Programming &#8211; avoid PHP Notices
Last modified: August 31, 2016

---

# Clean PHP Programming – avoid PHP Notices

 *  Resolved [Robert](https://wordpress.org/support/users/e2robert/)
 * (@e2robert)
 * [10 years ago](https://wordpress.org/support/topic/clean-php-programming-avoid-php-notices/)
 * Hello,
 * I’m using your plugin and it is great. However, there are some minor errors (
   in terms of clean programming violations) that always popup in our error log (
   the errors are not functional, but it is hard to grasp the real errors, if such
   PHP notices pollute the error log):
    1. Notice: Undefined index: PHP_AUTH_USER in wp-content/plugins/more-privacy-options/
       ds_wp3_private_blog.php on line 348
    2. Notice: Undefined index: PHP_AUTH_PW in wp-content/plugins/more-privacy-options/
       ds_wp3_private_blog.php on line 349
    3. Notice: Undefined property: WP_Error::$user_login in wp-content/plugins/more-
       privacy-options/ds_wp3_private_blog.php on line 352
    4. Deprecated: Methods with the same name as their class will not be constructors
       in a future version of PHP; ds_more_privacy_options has a deprecated constructor
       in wp-content/plugins/more-privacy-options/ds_wp3_private_blog.php on line 93
 * The solution to the first two points is easy; just replace:
    `$credentials['user_login']
   = $_SERVER['PHP_AUTH_USER'];` with: `$credentials['user_login'] = isset($_SERVER['
   PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';`
 * The third problem can be solved by replacing:
    `$user_id = get_user_id_from_string(
   $user->user_login );` with `$user_id = !is_wp_error( $user ) ? get_user_id_from_string(
   $user->user_login ) : null;`
 * And finally we need to use __construct to stay future proof. PHP4-styled constructors
   will be not called by future PHP versions. so we need to rename the method `ds_more_privacy_options`
   to `__construct`.
    Additionally we can add another method to retain backward 
   compatibility:
 *     ```
       function ds_more_privacy_options() {
       	$this->__construct();
       }
       ```
   
 * I hope you can understand why clean programming is important. If you got questions,
   feel free to contact me. I’ve also appended now a patch if that is easier for
   you:
 *     ```
       Index: wp-content/plugins/more-privacy-options/ds_wp3_private_blog.php
       IDEA additional info:
       Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
       <+>UTF-8
       ===================================================================
       --- wp-content/plugins/more-privacy-options/ds_wp3_private_blog.php	(revision 4)
       +++ wp-content/plugins/more-privacy-options/ds_wp3_private_blog.php	(revision )
       @@ -94,6 +94,10 @@
        		var $l10n_prefix;
   
        	function ds_more_privacy_options() {
       +		$this->__construct();
       +	}
       +
       +	function __construct() {
        		global  $current_blog;
   
        		if ( ! is_multisite() ) {
       @@ -181,7 +185,7 @@
        				$blogname = get_blog_option( $blog_id, 'blogname');
        			$email =  stripslashes( get_site_option('admin_email') );
        			$subject = __('Site ', $this->l10n_prefix).'"'.$blogname.'" (ID: '.$blog_id.'), '.get_site_url( $blog_id ).', '. __('changed reading visibility setting to ', $this->l10n_prefix) .'"'. $to_new.'"';
       -  			$message .= __('Site ', $this->l10n_prefix).'"'.$blogname.'" (ID: '.$blog_id.'), '.get_site_url( $blog_id ).', '.__('changed reading visibility setting to ', $this->l10n_prefix) .'"'. $to_new.'."';
       +  			$message = __('Site ', $this->l10n_prefix).'"'.$blogname.'" (ID: '.$blog_id.'), '.get_site_url( $blog_id ).', '.__('changed reading visibility setting to ', $this->l10n_prefix) .'"'. $to_new.'."';
          			$message .= __(" \r\n\r\nSent by More Privacy Options plugin.", $this->l10n_prefix);
   
        			$headers = 'Auto-Submitted: auto-generated';
       @@ -345,11 +349,11 @@
        		//December 2012 tested with "Free RSS" for iPhone. Google Reader does not authenticate locked feeds. Tough to find a free reader that does authenticate.
        		global $current_blog, $blog_id;
        			$credentials = array();
       -        	$credentials['user_login'] = $_SERVER['PHP_AUTH_USER'];
       -        	$credentials['user_password'] = $_SERVER['PHP_AUTH_PW'];
       +        	$credentials['user_login'] = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
       +        	$credentials['user_password'] = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
        			$credentials['remember'] = true;
        			$user = wp_signon( $credentials, false ); //if this creates WP_User, the next 3 lines are redundant
       -			$user_id = get_user_id_from_string( $user->user_login );
       +			$user_id = !is_wp_error( $user ) ? get_user_id_from_string( $user->user_login ) : null;
   
        			if ( is_wp_error( $user ) ||
        				// "Members Only"
       \ No newline at end of file
       ```
   
 * [https://wordpress.org/plugins/more-privacy-options/](https://wordpress.org/plugins/more-privacy-options/)

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Steve](https://wordpress.org/support/users/srg-1/)
 * (@srg-1)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/clean-php-programming-avoid-php-notices/#post-7387283)
 * Hey Robert,
 * Looks like there hasn’t been a response from the dev. Are you able to submit 
   a pull request with this patch on the [GitHub repo](https://github.com/wp-plugins/more-privacy-options)?
   That might get their attention.
 *  Thread Starter [Robert](https://wordpress.org/support/users/e2robert/)
 * (@e2robert)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/clean-php-programming-avoid-php-notices/#post-7387288)
 * pull request created at [Github Pull Request #1](https://github.com/wp-plugins/more-privacy-options/pull/1)
 *  Plugin Author [David Sader](https://wordpress.org/support/users/dsader/)
 * (@dsader)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/clean-php-programming-avoid-php-notices/#post-7387289)
 * Updated, no more notices, and tested with WP 4.6 beta.
 *  [Steve](https://wordpress.org/support/users/srg-1/)
 * (@srg-1)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/clean-php-programming-avoid-php-notices/#post-7387290)
 * Thanks a lot, David!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Clean PHP Programming – avoid PHP Notices’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/more-privacy-options.svg)
 * [More Privacy Options](https://wordpress.org/plugins/more-privacy-options/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/more-privacy-options/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/more-privacy-options/)
 * [Active Topics](https://wordpress.org/support/plugin/more-privacy-options/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/more-privacy-options/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/more-privacy-options/reviews/)

 * 4 replies
 * 3 participants
 * Last reply from: [Steve](https://wordpress.org/support/users/srg-1/)
 * Last activity: [9 years, 10 months ago](https://wordpress.org/support/topic/clean-php-programming-avoid-php-notices/#post-7387290)
 * Status: resolved