I am trying to make a base class ... tiny framework if you will just for practice
So I start with example of child class because it has less code !!
class User extends Base {
public $id ;
public $username ;
public $email ;
public $password ;
function __construct(){
$this->table_name = 'users';
$this->set_cols(get_class_vars('User'));
}
}
$u = new User;
$u->username = 'jason';
$u->email = 'j#gmail.com';
$u->insert();
Here is my Base class
class Base {
protected $table_name ;
protected $table_columns ;
protected function set_cols($cols){
unset($cols['table_name']);
unset($cols['table_columns']);
$this->table_columns = array_keys($cols);
}
public function insert(){
$colums = $values = array();
foreach($this->table_columns as $col )
{
if(!$this->$col) continue ;
$values[] = $this->$col ;
$colums[] = $col ;
}
$values = implode(',' , $values);
$colums = implode(',' , $colums);
echo $sql = "INSTER INTO ".$this->table_name ." ($colums)
VALUES ($values) ";
}
}
Here is the problem , I want to make filter or get method (basically reading from database) static and then return an array of objects from database data
class Base{
static function filter($conditions =array()){
$query_condition = $conditions ; // some function to convert array to sql string
$query_result = "SELECT * FROM ".$this->table_name ." WHERE $query_condition ";
$export = array();
$class = get_called_class();
foreach($query_result as $q )
{
$obj = new $class;
foreach($this->table_columns as $col )
$obj->$col = $q[$col];
$export[] = $obj;
}
return $export;
}
}
$users = User::filter(['username'=>'jason' , 'email'=>'j#gmail.com']);
Here is the problem , with filter as static function __construct in User class will not get called and table_columns, table_name will be empty
also in the filter method I can't access them anyway because they are not static ... I can make a dummy User object in the filter method and solve this problems but somehow it doesn't feel right
Basically I have a design problem any suggestion is welcomed
The problem is that the static object is not really "created" when you run statically.
If you want the constructor to run, but still in a static sort of way, you need a "singleton". This is where the object is created once and then you can re-use. You can mix this technique in a static and non-static way (as you're actually creating a "global" object that can be shared).
An example is
class Singleton {
private static $instance;
public static function getInstance() {
if (null === static::$instance) {
self::$instance = new static();
}
return self::$instance;
}
}
$obj = Singleton::getInstance();
Each time this gets the same instance and remembers state from before.
If you want to keep your code base with as few changes as possible, you can create yourself an "initialized" variable statically - you just need to remember to call it in each and every function. While it sounds great, it's even worse than a Singleton as it still remembers state AND you need to remember the init each time. You can, however, use this mixed with static and non-static calls.
class notASingletonHonest {
private static $initialized = false;
private static function initialize() {
if (!self::$initialized) {
self::$initialized = true;
// Run construction stuff...
}
}
public static function functionA() {
self::$initialize();
// Do stuff
}
public static function functionB() {
self::$initialize();
// Do other stuff
}
}
But read a bit before you settle on a structure. The first is far better than the second, but even then if you do use it, ensure that your singleton classes can genuinely be ran at any time without reliance on previous state.
Because both classes remember state, there are many code purists that warn you not to use singletons. You are essentially creating a global variable that can be manipulated without control from anywhere. (Disclaimer - I use singletons, I use a mixture of any techniques required for the job.)
Google "php Singleton" for a range of opinions and more examples or where/where not to use them.
I agree with a lot of your premises in your code and design. First - User should be a non static class. Second - Base base should have a static function that acts a factory for User objects.
Lets focus on this part of your code inside the filter method
1 $query_result = "SELECT * FROM ".$this->table_name ." WHERE $query_condition ";
2 $export = array();
3
4
5 $class = get_called_class();
6 foreach($query_result as $q )
7 {
8 $obj = new $class;
9
10 foreach($this->table_columns as $col )
11 $obj->$col = $q[$col];
12
13 $export[] = $obj;
14
15 }
The issue is that lines 1 and 10 are trying to use this and you want to know the best way to avoid it.
The first change I would make is to change protected $table_name; to const TABLE_NAME like in this comment in the php docs http://php.net/manual/en/language.oop5.constants.php#104260. If you need table_name to be a changeable variable, that is the sign of bad design. This will allow you change line 1 to:
$class = get_called_class()
$query_result = "SELECT * FROM ". $class::TABLE_NAME . "WHERE $query_condition";
To solve the problem in line 10 - I believe you have two good options.
Option 1 - Constructor:
You can rewrite your constructor to take a 2nd optional parameter that would be an array. Your constructor would then assign all the values of the array. You then rewrite your for loop (lines 6 to 15) to:
foreach($query_result as $q)
{
$export[] = new $class($q);
}
And change your constructor to:
function __construct($vals = array()){
$columns = get_class_vars('User');
$this->set_cols($columns);
foreach($columns as $col)
{
if (isset($vals[$col])) {
$this->$col = $vals[$col];
}
}
}
Option 2 - Magic __set
This would be similar to making each property public, but instead of direct access to the properties they would first run through a function you have control over.
This solution requires only adding a single function to your Base class and a small change to your current loop
public function __set($prop, $value)
{
if (property_exists($this, $prop)) {
$this->$prop = $value;
}
}
and then change line 10-11 above to:
foreach($q as $col => $val) {
$obj->$col = $val
}
Generally it is a good idea to seperate the logic of storing and retrieving the data and the structure of the data itself in two seperate classes. A 'Repository' and a 'Model'. This makes your code cleaner, and also fixes this issue.
Of course you can implement this structure in many ways, but something like this would be a great starting point:
class Repository{
private $modelClass;
public function __construct($modelClass)
{
$this->modelClass = $modelClass;
}
public function get($id)
{
// Retrieve entity by ID
$modelClass = $this->modelClass;
return new $$modelClass();
}
public function save(ModelInterface $model)
{
$data = $model->getData();
// Persist data to the database;
}
}
interface ModelInterface
{
public function getData();
}
class User implements ModelInterface;
{
public int $userId;
public string $userName;
public function getData()
{
return [
"userId" => $userId,
"userName" => $userName
];
}
}
$userRepository = new Repository('User');
$user = $userRepository->get(2);
echo $user->userName; // Prints out the username
Good luck!
I don't think there is anything inherently wrong with your approach. That said, this is the way I would do it:
final class User extends Base {
public $id ;
public $username ;
public $email ;
public $password ;
protected static $_table_name = 'users';
protected static $_table_columns;
public static function getTableColumns(){
if( !self::$_table_columns ){
//cache this on the first call
self::$_table_columns = self::_set_cols( get_class_vars('User') );
}
return self::$_table_columns;
}
public static function getTableName(){
return self::$_table_name;
}
protected static function _set_cols($cols){
unset($cols['_table_name']);
unset($cols['_table_columns']);
return array_keys($cols);
}
}
$u = new User;
$u->username = 'jason';
$u->email = 'j#gmail.com';
$u->insert();
And then the base class, we can use Late Static Binding here static instead of self.
abstract class Base {
abstract static function getTableName();
abstract static function getTableColumns();
public function insert(){
$colums = $values = array();
foreach( static::getTableColumns() as $col ){
if(!$this->$col) continue ;
$values[] = $this->$col ;
$colums[] = $col ;
}
$values = implode(',' , $values);
$colums = implode(',' , $colums);
echo $sql = "INSERT INTO ". static::getTableName() ." ($colums) VALUES ($values) ";
}
static function filter($conditions =array()){
$query_condition = $conditions ; // some function to convert array to sql string
$query_result = "SELECT * FROM ".static::getTableName() ." WHERE $query_condition ";
$export = array();
$columns = static::getTableColumns(); //no need to call this in the loop
$class = get_called_class();
foreach($query_result as $q ){
$obj = new $class;
foreach( $columns as $col ){
$obj->$col = $q[$col];
}
$export[] = $obj;
}
return $export;
}
}
Now on the surface this seems trivial but consider this:
class User extends Base {
public $id ;
public $username ;
public $email ;
public $password ;
final public static function getTableName(){
return 'users';
}
final public static function getTableColumns(){
return [
'id',
'username',
'email',
'password'
];
}
}
Here we have a completely different implementation of those methods from the first Users class. So what we have done is force implementation of these values in the child classes where it belongs.
Also, by using methods instead of properties we have a place to put custom logic for those values. This can be as simple as returning an array or getting the defined properties and filtering a few of them out. We can also access them outside of the class ( proper like ) if we need them for some other reason.
So overall you weren't that far off, you just needed to use static Late Static Binding, and methods instead of properties.
http://php.net/manual/en/language.oop5.late-static-bindings.php
-Notes-
you also spelled Insert wrong INSTER.
I also put _ in front of protected / private stuff, just something I like to do.
final is optional but you may want to use static instead of self if you intend to extend the child class further.
the filter method, needs some work yet as you have some array to string conversion there and what not.
Related
I have the following class:
class cls_Tip extends cls_getTips {
public static $result = null;
public static $event = null;
public static $market = null;
public static $participant = null;
public function __construct() {
}
public static function grab( $id ) {
global $wpdb;
$query = " SELECT * FROM " . $wpdb->prefix . "asp_tips WHERE id = '" . $id . "';";
$result = $wpdb->get_row( $query );
self::$result = $result;
}
public static function add_element_to_parent_array(){
parent::add_array_ellement( self::$result );
}
public static function return_static_variable( $variable_name ) {
return self::${ $variable_name };
}
public static function get_event() {
new cls_Event;
cls_Event::grab( self::$result->markets_id );
cls_Event::add_static_variable_to_parent();
}
public static function get_market() {
new cls_Market;
cls_Market::grab( self::$result->markets_id );
cls_Market::add_static_variable_to_parent();
}
public static function get_participant() {
new cls_Participant;
cls_Participant::grab( self::$result->participants_id );
cls_Participant::add_static_variable_to_parent();
}
public static function add_static_variable( $variable_name, $class ) {
self::${ $variable_name } = $class;
}
}
When I initiate this class as follows:
new cls_Tip;
cls_Tip::get( $record->id );
cls_Tip::get_participant();
$p = cls_Tip::return_static_variable( 'participant' );
... it works, but $p continues to have the same value, after creating another 'new cls_Tip' as outlined above (in a loop.)
Is this because a static variable can only be set once? And what would you recommend doing in this case?
Thanks very much for your advice.
Best regards,
Giorgio
PS: Here is the cls_Participant class:
class cls_Participant extends cls_Tip {
public static $result = null;
public function __construct() {
}
public static function grab( $id ) {
global $wpdb;
$query = " ... ";
$result = $wpdb->get_row( $query );
self::$result = $result;
}
public static function add_static_variable_to_parent() {
parent::add_static_variable( 'participant', self::$result );
}
}
Well, you see, when you call static properties/methods of a class, unless you explicitly handle the instantiation inside of the function that you call, the "static" instance is a completely separate instance from the instantiated one -
Thus, you should do this:
$class = new cls_Tip;
$class::get( $record->id );
$class::get_participant();
$p = $class::return_static_variable( 'participant' );
And that should give you the behavior you expect...
However, in this case, you don't need to use static methods/properties... you could do:
$class = new cls_Tip;
$class->get( $record->id );
$class->get_participant();
$p = $class->return_static_variable( 'participant' );
Furthermore, this is equivalent, in your last line:
$p = cls_Tip::$participant
You don't need the getter function, since the property is public...
BUT, to further illustrate my answer, do this:
cls_Tip::$result = "Static Result";
$alt = new cls_Tip();
$alt::$result = "Instantiated Property";
echo cls_Tip::$result;
echo $alt::$result;
So, ultimately, the lesson here is that if you are going to have several separate instances of a class, you don't really need to label everything as static -
There seems to be a lot of discourse on this topic - some people say you should almost never use static methods/properties in PHP, albeit they seem to have a proper place in some cases.
However, it is my personal opinion that you would be better off taking all the static stuff out of your class, which is going to have several instances.
Thus, you would end up using the -> operator instead of the static :: operator.
A case where the :: operator would be more appropriate would be if you wanted to have a static class that managed all your instances might look like this...
class clp_Tip{
static private $instances;
//[...]
public static function new_instance($name){
return self::$instance[$name] = new $this;
}
public static function get_instances(){
return self::$instances;
}
//[...]
}
//[...]
// example:
$classOne = cls_Tip::new_instance('name');
$classTwo = cls_Tip::new_instance('two');
echo count(cls_Tip::get_instances()); // 2
$classOne->doSomeFunction();
$classOne->someProperty = "foo";
}
There are plenty of debates over why not use :: - the simplest answer is simply for design purposes, but - it makes good sense to use the operators in the way they were made to be used -
and the static operators were made to be used without having to invoke a new instance -
So, that is good enough reason, in my opinion, to remove the static stuff from your class and use the -> operator.
i'm quite new to php and trying to learn. I have 2 similiar classes. I want to create that objects when i pass count and object type. I read some about factory pattern. Here is my factory class:
class AssetFactory
{
private static $table;
public static $objects = array();
public static function Create($asset,$count)
{
switch ($asset) {
case "Item":
self::$table = "items";
break;
case "Job":
self::$table = "jobs";
break;
}
$db = new Database();
$rows = $db->query("SELECT * FROM ".self::$table." LIMIT ".$count);
foreach($rows as $row)
{
self::$objects[] = new $asset($row);
}
return self::$objects;
}
}
and when i need 5 items i use:
$myItems = AssetFactory::Create('Item',5);
when i need some jobs i use:
$myJobs= AssetFactory::Create('Job',5);
item and job are that similiar classes. My question is here, as i said im trying to learn. Am i doing this right? Did i understand factory pattern right? Have any good documents about this(i read everything on php.net, got anything else).
There are few thing that seem wrong in this case.
First of all , there are two similar structures which are used for object creation:
factories: if object requires some sort of initialization before released for 'consumption'
builders: if before creating object you have to create bunch of other objects
Usually people do not distinguish between the two, and just call them "Factories". So these would be two case where you use a factory.
What you have right now does not fit the description. You are creating some sort of database connection, then getting some data, and then using it for creating a list of objects. This is not a reusable code.
It would be much better if the usage of factory would be something like this :
$connection = new PDO( .. blah.. );
$stmt = $connection->query( 'SELECT * FROM '.$type.' LIMIT '.$count );
$factory = new Factory;
$collection = $factory->buildCollection( $type, $stmt->fetchALL(PDO::FETCH_ASSOC) );
Of course, with factory class which implements this behavior.
Additionally , you might want to watch (assuming, that you haven't seen already) two videos on the subject:
Don't Look For Things!
Unit Testing
Global State and Singletons
I am far from an authority on the factory design pattern, but I usually delegate the instantiation to a subclass.
<?php
class AssetFactory
{
public static function Create($asset, $count)
{
$objects = false;
switch ( strtolower($asset) ) {
case 'item':
case 'job':
$class_name = 'Asset'.$asset;
$asset_obj = $class_name::getInstance();
$objects = $asset_obj->Create($count);
break;
default:
// Invalid asset
break;
}
return $objects;
}
}
class Asset
{
var $name = null;
var $table = null;
private static $instance = null;
private function __construct() {}
private function __clone() {}
public function Create($count)
{
$objects = array();
$db = new Database();
$rows = $db->query("SELECT * FROM ".$this->table." LIMIT ".$count);
if ( is_array($rows) ) {
foreach($rows as $row)
{
$objects[] = new Item($row);
}
}
return $objects;
}
public static function getInstance()
{
if ( empty($self::$instance) ) {
$class_name = __CLASS__;
self::$instance = new $class_name();
}
return self::$instance;
}
}
class AssetItem extends Asset
{
private function __construct()
{
$this->name = 'Item';
$this->table = 'item';
parent::__construct();
}
}
class AssetJob extends Asset
{
private function __construct()
{
$this->name = 'Job';
$this->table = 'job';
parent::__construct();
}
}
This is not really a problem but more of a question.
My question is how it's 'supposed to be' programmed.
It might not be too clear to explain, though.
So my question is; do I have to make multiple methods to retrieve data from a database?
For example, I have a table called FRUITS.
It contains the ID, name and date the fruit was added.
Now I want to get the name of the fruit based on a given ID, and later on in the script I want to get the date of the fruit as well.
Should I make one method such as get_fruit($id) which returns both the name and date, or two separate methods get_name($id) and get_date($id)?
Thanks in advance.
You should use one object which would contain all the required data. For example:
class Fruit {
protected ... variables;
public function getId() {...}
public function getDate() {...}
...
}
Also implementing __set and __get would be nice example of using full php potential.
You also may implement save() method (or extend database row class, such as Zend_Db_Table_Row.
So whole code would look like:
$fruit = $model->getFruid( 7); // $id = 7 :)
echo $fruit->id; // would call internally $fruit->__get( 'id')
echo $fruit->date;
// And modification:
$fruit->data = '2011-08-07';
$fruit->save();
EDIT: using separate methods to load certain data is useful (only?) when you need to load large amount of data (such as long texts) which is required only on one place in your code and would affect performance.
EDIT 2: (answer to comment):
__get and __set are called when you try to access undefined property of an object, for example:
class Foo {
public $bar;
public function __get( $name){
echo $name "\n";
return 'This value was loaded';
}
}
// Try to use
Foo $foo;
echo $foo->bar . "\n";
echo $foo->foo . "\n";
There are two "large" approaches to this that I know about:
// First use __get and __set to access internal array containing data
class DbObject {
protected $data = array();
public function __get( $propertyName){
// Cannot use isset because of null values
if( !array_key_exits( $propertyName,$this->data)){
throw new Exception( 'Undefined key....');
}
return $this->data[ $propertyName];
}
// Don't forget to implement __set, __isset
}
// Second try to call getters and setter, such as:
class DbObject {
public function getId() {
return $this->id;
}
public function __get( $propertyName){
$methodName = 'get' . ucfirst( $propertyName);
if( !method_exits( array( $this, $methodName)){
throw new Exception( 'Undefined key....');
}
return $this->$methodName();
}
}
To sum up... First approach is easy to implement, is fully automatized... You don't need large amount of code and sources would be pretty much the same for every class. The second approach required more coding, but gives you a better control. For example:
public function setDate( $date){
$this->date = date( 'Y-m-d h:i:s', strtotime( $date));
}
But on the other hand, you can do this with first approach:
class Fruit extends DbObject {
public function __set( $key, $val){
switch( $key){
case 'date':
return $this->setDate( $val);
default:
return parent::__set( $key, $val);
}
}
}
Or you can use total combination and check for getter/setter first and than try to access property directly...
here is the code how you can use the one function to get different field's value.
function get_fruit($id,$field = ''){
$sql = "select * from table_name where id = $id";
$result = mysql_fetch_object(mysql_query($sql));
if($field != ''){
return $result->$field;
}else{
return $result;
}
}
echo get_fruit(1,'field_name');
class data_retrieve
{
public $tablename;
public $dbname;
public $fieldset;
public $data_array;
public $num_rows
function __construct()
{
$this->tablename='junk';
$this->dbname='test';
$this->fieldset=array('junk_id');
}
function getData($where_str)
{
$this->data_array= array();
global $dbconnect, $query;
if($dbconnect ==0 )
{
echo" Already Connected \n ";
$dbconnect=db_connect("objectdb") or die("cannot connect");
}
$where_str;
if(empty($where_str))
{
$where_str=NULL;
}
else
{
$where_str= "where". $where_str ;
}
$query= "select * from $this->tablename $where_str";
$record= mysql_query($query) or die($query);
$recNo=mysql_num_rows($record);
$this->num_rows=$recNo;
while($row= mysql_fetch_assoc($record))
{
$this->data_array[]=$row;
}
mysql_free_result($record);
return $this->data_array;
}
class fruit extends data_retrieve
{
function __construct()
{
parent::__construct();
$this->tablename='fruit';
$this->fieldset=array('fruit_id','fruit_name','date');
}
}
then
in your file create a fruit object like
$str="fruit_id=5";
$fruit_data = new fruit();
$records=$fruit_data->getData($str);
to display
foreach($records as $row )
{
print <<< HERE
<label class='table_content' > $row[fruit_id]</label>
<label class='table_content' > $row[fruit_name]</label>
<label class='table_content' > $row[date]</label>
HERE;
}
I want to create properties that are set to mysql data.
class MyClass{
private $a = $r['a'];
private $b = $r['a'];
private $c = $r['c'];
}
I know this is incorrect syntax but I want you to get the idea.
I could create a method that returns a requested mysql data, but I don't want the function to be called for every single row.
You need to implement the magic method __get.
Something like:
class MyClass {
protected $_row = array();
public function __get( $name )
{
if (array_key_exists($name, $this->_row)) {
return $this->_row[$name];
}
return null;
}
public function __isset( $name )
{
return array_key_exists($name, $this->_row);
}
}
And you could used as:
$obj = new MyClass();
$obj->load(); // Or any method to load internal data
echo $obj->a . $obj->b;
Why reinvent the wheel ?
check this mysqli_result::fetch_object
I am a learner, I have a class db to help me connect and fetch results in mySQL.
$set = $db->get_row("SELECT * FROM users");
echo $set->name;
this way i use echo results outside a class.
Now i have created another class name user and it has this function
public function name() {
global $db;
$set = $db->get_row("SELECT * FROM users");
$this->name = $set->name;
}
after initializing the class user, when i try to echo $user->name i dont get expected results.
Note i have declared above var $name; in class user
I'm pretty concerned by several things I see here
The method name name() is terribly uncommunicative as to what the method is supposed to do. Remember, methods are actions - try to give them some sort of verb in their name.
Usage of global in a class (or even usage of global period) when you should be using aggregation or composition.
You don't show any execution examples, but I can only assume you never actually call User::name(), which is why your test is failing
Here's some code that addresses these concerns.
<?php
class DB
{
/* Your methods and stuff here */
}
class User
{
protected $db;
protected $name;
public function __construct( DB $db )
{
$this->db = $db;
}
public function getName()
{
if ( is_null( $this->name ) )
{
$set = $this->db->get_row( "SELECT * FROM users" );
$this->name = $set->name;
}
return $this->name;
}
}
$db = new DB();
$user = new User( $db );
echo $user->getName();
class DB
{
public function get_row($q)
{
# do query and store in object
return $object;
}
}
class User
{
public $name;
public function __construct()
{
$this->name();
}
public function name() {
global $db;
$set = $db->get_row("SELECT * FROM users");
echo "<pre>".print_r($set)."</pre>"; # make sure $set is returning what you expected.
$this->name = $set->name;
}
}
$db = new DB();
$user = new User();
echo $user->name;
I am very much sorry, i figured out that problem was on my part, i was using cookies and had two cookies set which were giving problems :(