[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 592
  • Last Modified:

PHP - using array_merge inside a PHP class

I'm having a hard time using array_merge ,

I get this error:


Notice: Undefined variable: mbad_badwords1 in /home/website/public_html/badwords/index_class.php on line 60

Fatal error: Cannot access empty property in /home/website/public_html/badwords/index_class.php on line 60

class CsvParser
{
	var $mWebServer;
	var $mcatproductpatterns;
	var $mSourceFile;
	var $mOutFile;
	var $mOutFileEbay;

	var $mEbayPicsLimit;
	var $mDomainName;

	var $mbad_badwords1;
	var $mbad_badwords2;
	var $mbad_badwords3;
	var $mbad_badwords4;
	var $mbad_badwords5;
	var $mbad_badwords6;

	var $mArrayOfBadWords;
	var $mContectionLink;
	var $catalogSections;

   //Constructor
   function CsvParser(){
	   
   	   /*Catalog range*/
   	   $this->catalogSections = "A-I-Q-Z";
   	
	   /*Server path*/
	   $this->mWebServer = "/home/website/public_html/media/import/";
	   
	   /*Include files for ebay description and materials*/
	   $this->mIncludeFiles = "/home/website/public_html/badwords/includes";
	   
	   $this->mDomainName = "http://www.website.com/media/import";
	   $this->mEbayPicsLimit = 35;
	   
	   /*Ebay URL*/
	   $this->mEbayURL = "/home/website/public_html/badwords/includes/";


		/* Bad Words */
		$this->mbad_badwords1 = array('apples');
		$this->mbad_badwords2 = array('oranges');
		$this->mbad_badwords3 = array('foo');
		$this->mbad_badwords4 = array('bar');
		$this->mbad_badwords5 = array('ben bernanke');
		$this->mbad_badwords5 = array('money printing');
		
	   //Array of bad Words only used for Ebay

$this->$mArrayOfBadWords = array_merge($this->$mbad_badwords1, $this->$mbad_badwords2, $this->$mbad_badwords3, $this->mbad_badwords4);

	   //Source file
	   $this->mSourceFile = "masterlist.csv";

	   //Magento output file
	   $this->mOutFile = "masterlist_output.csv";

		//ebay output file.
	   $this->mOutFileEbay = "masterlist_output_ebay.csv";

	   //Special procedure
	   $this->setProductPatterns();

   }

Open in new window

0
RonPaul
Asked:
RonPaul
3 Solutions
 
Ray PaseurCommented:
Uhh, OK -- what is the question?  The "var" reserved word went out of fashion a few years ago, when PHP 5 was introduced.
0
 
RonPaulAuthor Commented:
I am getting this error on line 52:

actually it should be line 52, how do I fix with to resolve the error?

Notice: Undefined variable: mbad_badwords1 in /home/website/public_html/badwords/index_class.php on line 52

Fatal error: Cannot access empty property in /home/website/public_html/badwords/index_class.php on line 52

(I had removed some commented code so my lines were off)
0
 
Ray PaseurCommented:
Please post the complete code so that the Notice and Error messages are aligned with the code, thanks.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
lwadwellCommented:
How about ... (with less $ signs):

$this->mArrayOfBadWords = array_merge($this->mbad_badwords1, $this->mbad_badwords2, $this->mbad_badwords3, $this->mbad_badwords4);
0
 
Ray PaseurCommented:
Please see http://www.laprbass.com/RAY_temp_ronpaul.php

In this example I have updated the code to reflect the PHP 5 OOP notation.  You can start your learning adventure here:
http://php.net/manual/en/language.oop5.php

This comment may be particularly applicable to your problem:
http://php.net/manual/en/language.oop5.properties.php#108320

<?php // RAY_temp_ronpaul.php
error_reporting(E_ALL);

// UPDATED FOR PHP 5 OOP NOTATION
class CsvParser
{
    // CLASS PROPERTIES
    protected $mbad_badwords1;
    protected $mbad_badwords2;
    protected $mbad_badwords3;
    protected $mbad_badwords4;
    protected $mbad_badwords5;
    protected $mbad_badwords6;

    protected $mArrayOfBadWords;

    // CLASS CONSTRUCTOR ASSIGNS VALUES TO THE PROPERTIES
    public function __construct()
    {
        // CREATE SOME ARRAYS
        $this->mbad_badwords1 = array('apples');
        $this->mbad_badwords2 = array('oranges');
        $this->mbad_badwords3 = array('foo');
        $this->mbad_badwords4 = array('bar');
        $this->mbad_badwords5 = array('ben bernanke');
        $this->mbad_badwords6 = array('money printing'); // NOTE: VARIABLE SUFFIX CHANGED FROM 5 TO 6

        // MERGE THE ARRAYS
        $this->mArrayOfBadWords
        = array_merge
        ( $this->mbad_badwords1
        , $this->mbad_badwords2
        , $this->mbad_badwords3
        , $this->mbad_badwords4
        , $this->mbad_badwords5
        , $this->mbad_badwords6
        )
        ;
    }

    // A GETTER METHOD TO RETURN THE MERGED ARRAYS
    public function readOut()
    {
        return $this->mArrayOfBadWords;
    }
}

// CREATE THE OBJECT AND DISCOVER ITS PROPERTIES
$x = new csvparser;
print_r($x->readout());

// SHOW ALL THE PROPERTIES
echo '<pre>';
var_dump($x);

Open in new window

HTH, ~Ray
0
 
rinfoCommented:
Firstly mbad_badwords1 is not defined anywhere in the scope of the script you are running.
Thats why you are getting variable not defined as well as cannot access empty property errors.
You should consider putting
      var $mbad_badwords1;
as public static $mbad_badwords1;
0
 
RonPaulAuthor Commented:
Great work ! THANKS FOR ALL THE ANSWERS!! :) :) :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now