creating a class instance

I think I have a scope problem

Page 1 of 1

12 Replies - 955 Views - Last Post: 11 January 2010 - 04:16 AM Rate Topic: -----

#1 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

creating a class instance

Posted 08 January 2010 - 07:40 AM

I have a class that can perform validation that I need two instances of.

At the moment I have a globalValidate instance in my siteEngine.php file. This is procedural based e.g. it doesn't have a class just some common functions available to everything

I cannot get an instance of the validator class in the userEngine however code snippets below:

siteEngine.php
		require_once('dbEngine.php');
	require_once('validator.php'); 
	require_once('userEngine.php');

	$dbEngine = new dbEngine();
	$globalValidate = new validator(); // this works fine



userEngine.php
class userEngine {

	var $validate = new validator(); //this won't work - line 9
		....



the error is Parse error: parse error in ~\htdocs\lib\userEngine.php on line 9

I think it's a scope issue but not entirely sure how to solve it.

Thanks
Danny

This post has been edited by danny_kay1710: 08 January 2010 - 07:41 AM


Is This A Good Question/Topic? 0
  • +

Replies To: creating a class instance

#2 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: creating a class instance

Posted 08 January 2010 - 07:43 AM

Maybe try taking the "var" off the front of it, also it could be one of your surrounding lines if uoi are missing a ; the line before or something like that.
Was This Post Helpful? 0
  • +
  • -

#3 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Re: creating a class instance

Posted 08 January 2010 - 07:55 AM

View PostMoshambi, on 8 Jan, 2010 - 01:43 PM, said:

Maybe try taking the "var" off the front of it, also it could be one of your surrounding lines if uoi are missing a ; the line before or something like that.


doesn't work without the var either had already tried, however the error does change to:

Parse error: parse error, expecting `T_FUNCTION' in ~\htdocs\lib\userEngine.php on line 9

There are no lines about without a semi-colon (its the first thing i checked for), I have checked for all the obvious things although it is easy to miss silly mistakes:

expanding on what I had originally - the ... does signal more code but nothing that is really applicable it can all be commented out without changing the error.

siteEngine:
<?php
class userEngine {

	var $loggedIn = '';
	var $username = '';
	var $accessLevel = 0;
	
	$validate = new validator();
	
	function __construct()
	{
	    ....
        }

}
?>



siteEngine.php
<?php

	require_once('dbEngine.php');
	require_once('validator.php');
	require_once('userEngine.php');

	$dbEngine = new dbEngine();
	$globalValidate = new validator();
	
	$siteTimeout = returnSetting("SITE_TIMEOUT");


	function getUniqueCode($length = "")
	{	
		...
	}

	function startDocument($pageTitle, $pageDescription, $pageKeywords)
	{
		...
	}

	function redirect($location, $script = false) 
	{
		...
	}

	function checkPage()
	{
		...
	}
	
	
?>



Just another point this is my index.php file which deals with all the requests
<?php
	session_start();
	define("SHOP", true);
	// Include the siteEngine and get the page name
	require_once('lib/siteEngine.php');
	$strPage=checkPage();
	// StartDocument($pageTitle, $pageDescription, $pageKeywords)
	echo startDocument('', "", "");
	// Include the header
	require_once("includes/header.php");
	// Include the pathway
	require_once('includes/pathway.php');
?>
    <div id="mainbody-padding">
      <!-- begin maincolumn -->
      <?php
	  	$validate->listErrors(",");
	  	require_once('includes/'.$strPage.'.php');
	  ?>
      <!-- end maincolumn -->
      <!-- begin sidecolumn -->
      <?php
	  	require_once('includes/sidebar.php');
	  ?>
      <!-- end sidecolumn -->
      <div class="clr"></div>
    </div>
  </div>
  <!-- end mainbody -->
  <!-- begin bottom section -->
  <div id="mainbody-bottom">
    <div id="mainbody-bottom2">
      <div id="mainbody-bottom3"> </div>
    </div>
  </div>
  <!-- end bottom section -->
  <div align="center">
      <?php
	  	require_once('includes/footer.php');
	  ?>
  </div>
</div>
<!-- end wrapper -->
</body>
</html>


This post has been edited by danny_kay1710: 08 January 2010 - 07:56 AM

Was This Post Helpful? 0
  • +
  • -

#4 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: creating a class instance

Posted 08 January 2010 - 07:59 AM

where is the constructor for validator() ? Is that it right below it or is that the constructor for 'userEnginge' ? Sorry familiar with OOP but has been a while with PHP so I forget the syntax a little.
Was This Post Helpful? 0
  • +
  • -

#5 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Re: creating a class instance

Posted 08 January 2010 - 08:08 AM

the constructor fuinction is __construct:

The validator.php file and class is as below:
<?php

class validator {

	var $errors = array();
	
	// Validate something's been entered
	function validateRequired($input ,$errorMSG = ''){
		if (trim($input) != "") 
		{
			return true;
		} else {
			$this->errors[] = $errorMSG;
			return false;
		}
	}
	
	// Validate text only
	function validateText($input, $errorMSG = ''){
		if (ereg("^[A-Za-z0-9\ ]+$", $input)) 
		{
			return true;
		} else {
			$this->errors[] = $errorMSG;
			return false; 
		}
	}

	// Validate text only, no spaces allowed
	function validateTextNoSpaces($input, $errorMSG = ''){
		if (ereg("^[A-Za-z0-9]+$", $input))
		{
			return true;
		} else {
			$this->errors[] = $errorMSG;
			return false; 
		}
	}
		
	// Validate email address
	function validateEmail($input, $errorMSG = ''){
		if (ereg("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $input))
		{
			return true;
		} else {
			$this->errors[] = $errorMSG;
			return false; 
		}
	}
	
	// Validate numbers only
	function validateNumber($input, $errorMSG = ''){
		if (is_numeric($input)) {
			return true; // The value is numeric, return true
		} else { 
			$this->errors[] = $description; // Value not numeric! Add error description to list of errors
			return false; // Return false
		}
	}
	
	// Validate date
	function validateDate($input, $errorMSG = ''){
		if (strtotime($input) == -1 || $input == '') 
		{
			$this->errors[] = $description;
			return false;
		} else {
			return true;
		}
	}
	
	// Check whether any errors have been found (i.e. validation has returned false)
	// since the object was created
	function foundErrors() {
		if (count($this->errors) > 0){
			return true;
		} else {
			return false;
		}
	}

	// Return a string containing a list of errors found,
	// Seperated by a given deliminator
	function listErrors($delim = ' '){
		return implode($delim,$this->errors);
	}
	
	// Manually add something to the list of errors
	function addError($description){
		$this->errors[] = $description;
	}
	
}

?>



I dont actually specify a constructor as I don't need it to do anything, the thing is though it works inside of siteEngine.php but not userEngine.php.

validator is included in siteEngine as is userEngine. Validtor is included before userEngine but for some reason userEngine can't seem to see validator but it does work inside of siteEngine. Does that make sense?

This post has been edited by danny_kay1710: 08 January 2010 - 08:11 AM

Was This Post Helpful? 0
  • +
  • -

#6 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: creating a class instance

Posted 08 January 2010 - 08:10 AM

I just noticed that at the top of your siteEngine.php file you don't have a require_once('validator.php'); call, that may do the trick.
Was This Post Helpful? 0
  • +
  • -

#7 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Re: creating a class instance

Posted 08 January 2010 - 08:15 AM

Doesn't help which I didn't think it would because it is already included through siteEngine.php if that makes sense?

It could work if I call require() twice but I would rather not e.g. why I was using require_once() since it only allows it to be included once.

EDIT: Also just a note, it also doesn't work if I just declare the var and then try to instantiate it in the constructor of the userEngine class. It's got to be a scope/permission thing or relevant to the order in which PHP processes it concerning OOP.

This post has been edited by danny_kay1710: 08 January 2010 - 08:17 AM

Was This Post Helpful? 0
  • +
  • -

#8 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: creating a class instance

Posted 08 January 2010 - 08:24 AM

Oh ok ya that makes sense, duh. But I am guessing you must be right about it being scope, I will create a test of my own here and see if I can come up with anything to why it's not working.
Was This Post Helpful? 0
  • +
  • -

#9 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: creating a class instance

Posted 08 January 2010 - 08:56 AM

Wow, this is really a bizarre error. The only solution that I could come up with so far (it seemed to work, as I did not get any errors) was to just change where you declare this variable.

I changed it to right below your other one:

$globalValidate = new validator();
$validate = new validator();



I'll try to keep looking but I really don't see any reason why it doesn't work, unless it is a scope thing and it's because it's declared inside of the class.
Was This Post Helpful? 1
  • +
  • -

#10 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Re: creating a class instance

Posted 08 January 2010 - 03:58 PM

I thought it was strange as well, being familiar with more languages than php I could not see a reason for it not working.

I, at first thought I was missing something silly but when I couldn't see it decided to post it here. I am working on this at work so won't be able to test anything else major until Monday morning UK time.

Thanks for your help
Danny
Was This Post Helpful? 0
  • +
  • -

#11 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Re: creating a class instance

Posted 11 January 2010 - 02:26 AM

View PostMoshambi, on 8 Jan, 2010 - 02:56 PM, said:

Wow, this is really a bizarre error. The only solution that I could come up with so far (it seemed to work, as I did not get any errors) was to just change where you declare this variable.

I changed it to right below your other one:

$globalValidate = new validator();
$validate = new validator();



I'll try to keep looking but I really don't see any reason why it doesn't work, unless it is a scope thing and it's because it's declared inside of the class.


The problem with doing that is I really don't want the second validator to be accessible by anything else.

I still don't know what I can do to solve this!!

EDIT: Needing to move on until I can find a more suitable fix, I tried your solution however it now errors with:

Parse error: parse error in ~\htdocs\lib\userEngine.php on line 21

The offending code:
$userValidate->validateEmail($_REQUEST['txtEmail'], 'You must enter a valid email address to login');
$userValidate->validateRequired($_REQUEST['txtPass'], "You must enter a password to login")
			
if (!$userValidate->foundErrors()) //errors here
{
	   ...
}



I have tried multiple incarnations including
if (!($userValidate->foundErrors())) 
&
if ($userValidate->foundErrors()==false)
even assigning it to a var and using the var in the if statment.

This post has been edited by danny_kay1710: 11 January 2010 - 03:46 AM

Was This Post Helpful? 0
  • +
  • -

#12 przemass  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 166
  • Joined: 18-July 09

Re: creating a class instance

Posted 11 January 2010 - 03:48 AM

Try:
class UserEngine {
  private $validate;

  function __construct() {
	$this->validate = new validator();
  }


Was This Post Helpful? 1
  • +
  • -

#13 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Re: creating a class instance

Posted 11 January 2010 - 04:16 AM

I got it working, przemass your almost there. This is the working code:

class userEngine {
  var $userValidate;
  
  function __construct()
  {
	 global $userValidate;
	 $userValidate = new validator();
  }

}



Thanks for your help guys!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1