I’m new to OOP and trying to define a set of public prepared SQL queries that can be accessed in the methods of the classes. What I’ve found so far though, is that other public variables of the class can’t be inserted inside another public variable. I’m completely open to any other way to do this. Below is the code I’m looking to use:
class baseQuery
{
public static $ignoreAccounts = "(1, 2, 3, 4)";
public $preparedQuery = "SELECT COUNT(?) FROM ? WHERE removed != 1 AND accountid NOT IN static::$ignoreaccounts AND ? BETWEEN $dateAfter AND $dateBefore";
But I’m getting Parse error: syntax error, unexpected ‘”‘
I’d abstract it even further. Create public methods that get the data you are trying to get, not that contain general sql statements.
so perhaps a
retrieveAccountsmethod. That takes an array of accounts to ignore.Inside the method you can add the ignored accounts.
Sorry, to clarify, having your current setup doesn’t necessarily provide all the advantages of OOP (not that my example below does either but hopefully it will help you a direction)
Perhaps your app can benefit from an
AccountManagerMaybe your AccountManager class should have an
addAcountmethod too? I don’t know.I believe it is good practice to think in terms of there being a “consumer” of your classes, Even if that person is you. The consumer doesn’t need to know how you are getting your account information. They only need to be able to do it. And like i said this isn’t necessarily the way to do it either but its stronger abstraction.
So with my suggestion above what if for some reason you want to pull account numbers from a file, or postgres, or mongodb. You can build a program using this class and if you need to change where you are getting accounts from you do it in the AccountManager and your program doesn’t need to change at all.