Modify stored object - php

I have two classes, with one storing multiple references to the other. After I create a new instance of the second class, I might have to change properties on it later and I would like it to update all references to that object. Is there a way to make the object instances always pass by reference so that when a property gets updated in the object it also updates all other references to it?
class Users {
private $members = array();
public function addMember($id) {
$member = new Member($id);
array_push($members, $member);
return $member;
}
}
class User {
public $id;
__construct($id) {
$this->id = $id;
}
public function addProperty($propName, $propValue) {
$this->{$propName} = $propValue;
}
}
So currently with this setup I can create a new member and add them to the members array:
$users = new Users();
$user = $users->addMember(1);
But if I try to add a new property to the member as so:
$member->addProperty("job_title", "developer");
The property doesn't appear in the members array because the member is not being passed by reference.

See this:
public function addMember($id) {
$member = new Member($id);
....
Here you create an object of class Member, but I think you want
$member = new User($id);

Related

Laravel how save a model to the database as a new one?

Because of unit testing our classes looks like this:
class MyObserverClass
{
private $model;
public function __construct(Model $model)
{
$this->model= $model;
}
public function saved(OtherModel $otherModel)
{
//Some condition
while(...) {
//$this->model->exists = false;
$this->model->property = "x-value";
$this->model->sub_id = $otherModel->id;
//Other props...
$this->model->state = model::STATE;
$this->model->save();
}
}
But when I run this, the same model get overridden x times, and only the last one will be in the database.
How I can force laravel to save the model as a new one in the database? I was trying with the exists property but I couldn't make it work.
You can use newInstance method to create new object instance:
while(...) {
$model = $this->model->newInstance();
$model->property = "x-value";
$model->sub_id = $otherModel->id;
//Other props...
$model->state = model::STATE;
$model->save();
}
However if you create many same or similar object in this loop, you could use replicate method to create one base object and to change only some properties.
try this
while(...) {
$model = new self;
$model -> name = $name;
....
$model -> save();
}
or you can pass an array to insert to create method
// Create a new user in the database...
$user = Model::create(['name' => 'John']);

Switching Classes during __construct()

Let's take the following classes for a moment:
class member {
public $id;
public function __construct($id) {
$this->id = $id;
// Check user level
if ($this->check_user_level() == moderator) {
$this = new moderator($this->id);
}
}
private function check_user_level() {
// Return user level from system based on user ID
}
}
class moderator extends member {
public function show_moderator_tools() {
// etc.
}
}
==================
$user = new member($user_id);
The desired behavior is to have $user detect whether the user is authorized to have moderator access, and if so recast the user using the moderator class instead of member.
PHP forbids simply reassigning $this, so what appears to be the best solution would be one of the following:
Run check_user_level($id) as a regular function and using an if statement
$user_level = check_user_level($id);
if ($user_level == "moderator") {
$user = new moderator($id);
} else {
$user = new member($id);
}
Set a flag in the base class that can be used for a check/redefinition after initializing $user
$user = new member($id);
if ($user->flag = TRUE) {
$user = new moderator($id);
}
The latter feels like it's introducing a security flaw, especially since the flag (which could just as easily be $user->user_level, or similar, I guess) would have to be public to be able to check it afterward.
What I would like to do would be to just make one call to new member($id) and have it handle things automatically from there, without the need for if statements after the fact. Is there a way to do this?
You can of do this by introducing another class (lets call it user) and using the __call magic method in php and call_user_func_array for calling the methods.
The logic is something like this -
Create a user class that has no method except check_user_level. It checks proper details and assigns it's $obj to the instance of either member or moderator class.
Here is how the classes would look like (I've changed the functions to print something out)-
class user{
public function __construct($id) {
$this->id = $id;
if ($this->check_user_level() == "moderator") {
$this->obj = new moderator($this->id);
}else{
$this->obj = new member($this->id);
}
}
public function __call($method, $args){
call_user_func_array(array($this->obj,$method), $args);
}
public function __get($prop){
if(isset($this->obj->$prop)){
return $this->obj->$prop;
}
return NULL;
}
private function check_user_level() {
// Return user level from system based on user ID
if($this->id == 1){
return "moderator";
}
return "member";
}
}
class member {
public $id;
public function __construct($id) {
$this->id = $id;
}
public function show_message($arg){
var_dump($this->id.$arg);
}
}
class moderator extends member{
public function show_moderator_tools() {
var_dump($this->id ."My toolset!");
}
}
So, now you can simply call the user class and that will automatically decide if it's a member or a moderator and call the method if it exists.
//Trying out a few examples
//Creating a member user
$mem_obj = new user(213);
$mem_obj->show_message("New message");
//Creating a moderator user
$mod_obj = new user(1);
$mod_obj->show_moderator_tools();
/*
OUTPUTS
-------
string(14) "213New message"
string(12) "1My toolset!"
*/
But you need to be careful with these kind of hacks.
For instance -
//This will fail
//because mem_obj doesn't have show_moderator_tools()
$mem_obj->show_moderator_tools();
EDIT
You can similarly go ahead with redirecting to properties using __get.
I have modified the code above to add this method beneath __call.
//Testing it
var_dump($mem_obj->id);
//This is an illegal property
var_dump($mem_obj->illegelProperty);
/*
OUTPUTS
int(213)
NULL
*/

Is there a way to set only defined properties when using fetch_object on a mysqli_result?

If I have a class:
class ExampleClass {
private $thing1;
private $thing2;
}
I can use fetch_object('ExampleClass') on a mysqli_result object to create an instance of ExampleClass. Using this method, the private properties of ExampleClass will be set, provided the query has columns with the same names.
In addition to those properties, other public properties will be added for any other columns in the query. Is there any way to avoid this? I couldn't find anything about it in the php documentation for fetch_object.
If I set up ExampleClass with a constructor like this
function __construct($properties = []) {
foreach ($properties as $key => $value)
if (property_exists($this, $key))
$this->$key = $value;
}
I can get the row from the result set using fetch_assoc instead of fetch_object, and then create a new ExampleClass with the resulting array as an argument. This achieves what I am going for, but I was hoping for something more direct.
For one of my projects I have built a system just like that.
All classes are derived of the basic abstract Object class which, among others, offers a cloneInstance() method. Then, in the concrete implementing class, I'm simply going to use it ('I'm assuming that $pdo is somehow accessible here, for brevity):
Note that cloneInstance() uses reflection to check if the target instance actually has a euqlly named property ($drfl->hasProperty()).
abstract class Object {
protected function cloneInstance($obj) {
if (is_object($obj)) {
$srfl = new ReflectionObject($obj);
$drfl = new ReflectionObject($this);
$sprops = $srfl->getProperties();
foreach ($sprops as $sprop) {
$sprop->setAccessible(true);
$name = $sprop->getName();
if ($drfl->hasProperty($name)) {
$value = $sprop->getValue($obj);
$propDest = $drfl->getProperty($name);
$propDest->setAccessible(true);
$propDest->setValue($this,$value);
}
}
}
return $this;
}
class MyAutomaticClass extends Object {
// static loader
public static function load($id) {
$result = null;
$sql = 'SELECT * FROM mytable WHERE id=:id';
$stmt = $pdo->prepare($sql, array(':id' => $id));
$list = $stmt->fetchAll(PDO::FETCH_OBJ);
if (count($list)) {
$result = new MyAutomaticClass($list[0]);
}
return $result;
}
// constructor makes use of base Objects cloning feature
public function __construct($obj=null) {
if (is_object($obj)) {
$this->cloneInstance($obj);
}
}
}

Object collection classes or not

I'm trying to decide whether to create many classes for each content type I have in my application/database or just stick with procedural code.
Version 1:
make a class for each object collection:
class App{
protected $user_collection;
function getUserCollection(){
if(!isset($this->user_collection)
$this->user_collection = new UserCollection($this);
return $this->user_collection;
}
// ...
}
class UserCollection{
function __construct(App $app){
$this->app = $app;
}
function getUser($user){
return new User($this->app, $user);
}
function getUsers($options){
$users = $this->app->getDatabase()->query($options);
foreach($users as &$user)
$user = new User($this, $user);
return $users;
}
// ...
}
which I'm using like:
$app = new App();
echo $app->getUserCollection()->getUser('admin')->email_address;
version 2:
keep all methods in a single class
class App{
function getUsers($options){
$users = $this->getDatabase()->query($options);
foreach($users as &$user)
$user = new User($this, $user);
return $users;
}
function getUser($user){
return new User($this, $user);
}
// ...
}
used like:
$app = new App();
echo $app->getUser('admin')->email_address;
version 3:
make getUsers() a a static method in the "User" class (the method instantiates a new User object):
$app = new App();
echo User::getUser($app, 'admin')->email_address;
Which way should I go? The "user" object is just an example, App has other objects too, like "database", "pages" etc.
I would use your version 1, but I would make getUser() and getUsers() methods of App.
This gets rid of the awkward getUserCollection() call, because instead inside the getUser() and what not you just call $this->user_collection.
Personnaly, I often used the second one with method like this:
class user {
/**
* Load object from ...
*/
public function load($userId) {}
/**
* Insert or Update the current object
*/
public function save() {}
/**
* Delete the current object
*/
public function delete() {
// delete object
// Reset ID for a future save
$this->UserID = null;
}
/**
* Get a list of object
*/
public static function getList() {
// Make your search here (from DB)
// Put rows into new "SELF" object
$list = array();
foreach($rows as $row) {
$obj = new self();
$obj->populate($row);
$list[$obj->UserID] = $obj; // Associative array or not...
}
}
}
Like you can see, I set my "getList" function static to simply access like this:
$listUsers = user::getList();
OK, it's very simple but work in most case of simple app.

PDO: Fetch class option to send fields to constructor as array

I am wondering whether or not it is possible to elegantly map the results of a PDO query to an array member in a class rather than have them floating about as public properties of that object.
Say I have the (condensed) following:
class DBObject {
protected
$record = array();
function __construct(array $record) {
if(!empty($record)) {
$this->loadRecord($record);
}
}
}
Ideally, I want to call the constructor with an array of values passed from the database, rather than use __set or any other weird methods. So using PDO's existing API would be great.
My rough get_all function at the moment has got this far:
static function get_all() {
$class = get_called_class();
$results = DB::factory()->query('SELECT * FROM ' . $class . ' ORDER BY ID');
$results->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, $class);
return $results;
}
NB: I'm running PHP 5.3 and MySQL through PDO, and already know this problem is solveable using __set, but I explicitly want to avoid using it in favour of something more performant.
You don't need to pass arguments to a constructor to make a class with private members using PDO::FETCH_CLASS. You can do something like this:
<?php
class Songs
{
private $artist;
private $title;
public function __construct()
{
}
public function get_artist()
{
return $this->artist;
}
public function get_title()
{
return $this->title;
}
private function set_artist($artist)
{
$this->artist = $artist;
}
private function set_title($title)
{
$this->title = $title;
}
}
I'm actually doing that on a demo site that I built. It works just fine with PDO::FETCH_CLASS. By default, FETCH_CLASS creates objects by populating the fields BEFORE the constructor. Think of it as bypassing the constructor. And it will do this with private members.
If you'd rather pass arguments to the constructor you can do your query like this:
$obj = $statement->fetchALL(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, 'Songs', $params);
In that case your constructor would look like this:
public function __construct($params)
{
$this->artist = $params[0]['artist'];
$this->title= $params[0]['title'];
}
Removed previous code
Right, can't you do something like this:
class DBObject {
protected $record = array();
function __construct($record = null) {
if(null === $record){
$obj_vars = get_object_vars($this);
$cls_vars = get_class_vars(get_class($this));
$this->$record = array_diff_key($obj_vars, $cls_vars);
}else{
$this->record = $record;
}
}
}
The problem with this however is that the values are still available as public members.
But what it will do is compare 'pre-defined' (class) members to the actual (object) members.
Since PDO will create new members in the object you can use array_diff_key to get the 'new' members.
Yes, this will still not pass them through your constructor.
How about using magic __set() method:
<?php
class MyClass
{
protected $record = array();
function __set($name, $value) {
$this->record[$name] = $value;
}
}
$pdo = new PDO("mysql:host=localhost;dbname=db", 'user', 'password');
$results = $pdo->query('SELECT * FROM table');
$results->setFetchMode(PDO::FETCH_CLASS, 'MyClass');
PHP will call this magic method for every non-existent property passing in its name and value.

Categories