Jump to content

Creating an object


joesaddigh

Recommended Posts

Hi,

 

I have decided to try and write some OO php.

 

I have a simple class with one function. I am trying to instantiate an object of that class using the new operator but get an error.

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in C:\wamp\www\School\addcourse.php on line 38

	
<?php 
    $CDBController = new CMasterDataController; //THIS IS LINE 38
    CMasterDataController->PerformQuery("SOME SQL HERE");
?>

 

I am including the file that contains this class (CMasterDataController) in this file (addcourse.php). The version of PHP i am using is 5.3.8.

Does anybody know what I am missing?

 

Thanks,

Joe

 

Link to comment
Share on other sites

Ah yess..  What an idiot I am. I want to try and put some of the common tasks that I perform regarding DB functions into a class which I can just create and use all over the place. The thing I am finding difficult to get used to is not having to specify data types and knowing what a function like:

 

mysql_query

mysql_fetch_array

 

returns? Also when you declare a function within a class you dont say perhaps:

 

ReturnType function NameOfFunction( param1, 2, 3 )

{

    return something;

}

 

You can simply do this:

 

function NameOfFunction( param1, 2, 3 )

{

    return something;

}

 

So if I wanted to return the result of a mysql_fetch_array from a function that takes sql as a param and then use this in the caller of the func how woud I loop through the result that I would expect to be returned?

 

Thanks for your help.

Link to comment
Share on other sites

To know what those functions return you look them up in the PHP manual. After a couple times you'll remember.

mysql_query. Returns a resource (SELECT query) or true (not a SELECT query) or false (error).

mysql_fetch_array. Returns an array (a row from the result) or false (no more rows).

 

 

PHP is loosely-typed which means you do not specify type names for variables. There is type hinting for function parameters, but that's a different (though related) thing.

If you only need to return one row from the query then make your NameOfFunction use a mysql_query() + mysql_fetch_array(), returning the result of the latter.

function QueryOne($sql) {
$result = mysql_query($sql);
if ($result) {
	return mysql_fetch_array($result);
} else {
	// error
	return $result;
}
}

If you want multiple rows then you'll need a loop.

function QueryMany($sql) {
$result = mysql_query($sql);
if ($result) {
	$results = array();
	while ($line = mysql_fetch_array($result)) $results[] = $line;
	return $results;
} else {
	// error
	return $result;
}
}

Link to comment
Share on other sites

That' excellent. Looking forward to creating some useful classes that I can re-use. One more thing if you don't mind. What do you use to debug PHP and is this easy to setup (know of any tutorials). I have tried setting up Zend studio before but not had a lot of luck so am using Dreamwever for now which means that I simply echo variable contents as and when I need to debug.

 

Sorry that this is unrelated but would appreciate any help on this.

 

Thanks again!  :D

Link to comment
Share on other sites

I personally don't use any tools. Because of how PHP works it's generally easier for me to echo/die a few values here and there and re-run the script.

The classic example is dealing with SQL queries:

// complicated stuff to build a $query
echo "\n"; // inserted when $query seems wrong, removed when I figure it out
$resultset = mysql_query($query);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.