php calling a function through with autoloader - php

These are my classes
class DB{
// returns the instance as it should - all system works well
public function create(){
// creates a row in the db....
}
}
class User{
// does all it does....
}
I have an autoloader that loads the classes and the system works well
$user = new User();
$user->create(....)
I want to use the method create from the DB while instantiating User class, how can I do so?
the thing that I want to prevent is not having to need to create a function "update" in every class which needs to be updated
For example:
Class user - update "users table"
class kids - update "kids table"
instead
// from DB class
update($table_name, $field)

The autoloader is responsible for finding the and including the files that contain the class definitions so you don't have to explicitly call include 'db.php'; etc. , but it isn't going to be able to do what you're trying to do here.
If the create() method is defined in the DB class, there's no reason for it to be accessible through an instance of a different class like User, and unless you specifically do something to allow it, the autoloader won't help with that.
The only way for you to access the DB::create() method from an instance of User is for User to extend DB.
class User extends DB { ...
This means that all User objects will also be instances of DB and have access to all of the public and protected methods of DB, not just create().
It looks like this might be what you want. If that's the case, it would be helpful for you to read up on Object Inheritance.

Related

how to run custom code after every $model->save() function in yii2

I want to execute my custom code after or before every $model->save() in Yii2.
I want to perform this globally like using components, etc.
I want to create a user activity log to store how many times a user insert or update any rows in database table, so for this I want to run some code when ever data inserted or update in tables.
Any help or suggestion will appreciated.
As #patryk mentioned ActiveRecord has beforeSave and afterSave methods.
I use something like the following to store a created date for new records (and updated date when existing records are updated). The code in the example is, of course, trivial but it allows you to use any arbitrary code you need, see the layout and how to split code for 'new' records and existing.
This overridden method can be added to any model class which extends ActiveRecord to allow the parent beforeSave to be called correctly also.
/**
* #inheritdoc
*/
public function beforeSave($insert)
{
if ($insert) {
// This is a new instance of modelClass, run your 'insert' code here.
$this->created_date = time();
}
// Anything else will be run any time a model is saved.
$this->updated_date = time();
return parent::beforeSave($insert);
}
edited to add:
if the code to be run is the same for each model you could create a trait and use the trait in each model to allow you to change the behaviour in one place. Or create a custom ActiveRecord class to override the beforeSave method for each subclass.
Create new class(MyActiveRecord) which extends \yii\db\ActiveRecord
Use extends MyActiveRecord to all your project models
Ex:
class MyActiveRecord extends \yii\db\ActiveRecord
{
public function afterSave($insert, $changedAttributes){
//This will called after every model saved
return parent::beforeSave($insert,$changedAttributes);
}
}
In your project other models
class Customer extends app\models\MyActiveRecord
{
}
Yii2 ActiveRecord class has beforeSave and afterSave methods. https://github.com/yiisoft/yii2/blob/master/framework/db/BaseActiveRecord.php#L926
But maybe it would be better to do such operation on database triggers?

Accessing other model file within another model file

I am trying to create an class inside the model directory. This class(eg:- Admin) exposes only the methods which makes sense to the controller.
The Admin class will do all the joins and stuffs internally on tables(using ORM) and prepares data which can be readily consumed by the controller.
I have created 15 files in the model directory each of them representing a table in my database using the ORM method.
Now I want to create to create an instance of table within the Admin classes' get_All() method. I have tried to use Kohana::factory() which was unavailable in my Admin class. I tried to create the instance using the 'new', but it ended in an error which says that the specified class is not found.
My class definition for Admin is as follows
<?php defined('SYSPATH') or die('No direct script access.');
class Model_Admin {
public function get_All()
{
$PD = new Model_PayPalData;
echo 'Success';
}
}
The error is:
ErrorException [ Fatal Error ]: Class 'Model_PayPalData' not found
APPPATH/classes/Model/admin.php
Please advice on how to deal with this situation.
Thanks for your attention
Seem like your class is not loaded. You can check this by viewing the declared classes
get_declared_classes();
If it is't there, make sure to include it, or add it to the Models directory, if needed without extending the ORM class.

How does OpenCart access its library classes?

Im current trying to learn more about the core of OpenCart and how its classes actually work. Im also trying to advance my OOP skills in general as Im still learning in that area, so perhaps theres something obvious that Im not seeing.
Im wondering how a controller file knows how to find the cart class (for example).
E.g.
In catalog/controller/checkout cart there is (obviously with code removed)
class ControllerCheckoutCart extends Controller {
public function index() {
$this->cart->update();
}
}
The Controller class can be found in system/engine/controller.php
update() can be found system/library/cart.
I assumed that in the controller.php there would be a link to the cart class, or an object made from it. (Im basing that on the use of $this->).
So how is the cart class actually found from the controller?
Thank you
Firstly, your ControllerCheckoutCart extends the Controller class, so this is the class we need to focus on. You can find this class in /system/engine/controller.php.
Inside this class, there are two magic methods we are interested in. The first is the __construct, where the "registry" class is loaded (found in /system/engine/registry.php if you're interested in picking that apart - it's very simplistic).
You can think of this as a lookup of all the classes the store uses, such as model files, library files and so on. In the construct, the registry is passed to the controller so it has a reference to it
public function __construct($registry) {
$this->registry = $registry;
}
The second and more important magic method is the __get method. This is called when a classes property doesn't exist, for you to handle it yourself if you wish to do so. OpenCart uses this to try and get the class with that key from the registry
public function __get($key) {
return $this->registry->get($key);
}
So $this->cart in any controller would try to get the object with the key cart from the registry. If you look at the index.php file you will see this is allocated in there
// Cart
$registry->set('cart', new Cart($registry));
ControllerCheckoutCart extends Controller, which means it inherits all the code in Controller which you are not seeing here. Some code in Controller, likely in Controller::__construct, is creating the $this->cart object. Example:
class Controller {
public function __construct() {
$this->cart = new Cart;
}
}
Since this constructor is inherited by all child classes, they construct their own $this->cart as well and have access to it in their own methods.
As mentioned by Jay Gilford, you need to register your newly added library class file in the index.php and/or admin/index.php (depending on if you are using it in catalog or admin)
$registry->set('yourlibraryclass', new YourLibraryClass());
so that upon system loading, Opencart knows that your class exists, then you can call all its functions by:
$this->yourlibraryfilename->function();
Please note that your library file name is normally the same as your class name, hence it is used in the example here.
After the change has been done in the index.php files, you need to logout and login again to see the changes.

Extended class does not see property of parent Codeigniter

I call a CodeIgniter controller method -imgupload- from jquery ajax. This controller extends my custom front controller.
class newad extends My_Controller{
public function __construct() {
parent::__construct();
}
public function imageupload() {
$this->load->library("uploadhandler");
}
The imgupload method calls the uploadhandler class which extends from newad.
class uploadhandler extends newad {
The functionality of that class works properly, except for one thing, I cant access the properties of the My_Controller class, even though they are declared protected.
The inheritance chain looks like this: My_Controller->newad->uploadhandler.
Any idea why I cant access those properties?
In short the answer is you do not need to extend Controller class here. You can just pass the value to your library as a parameter.
$params = array('user_ud' => $this->userID, 'otehr' => 'other');
$this->load->library('uploadhandler', $params);
//Now from your library
class uploadhandler{
public function __construct($params)
{
// Do something with $params
}
//.. Your code...//
}
Now about your question:
The functionality of that class works properly, except for one thing, I cant access the properties of the My_Controller class, even though they are declared protected. The inheritance chain looks like this: My_Controller->newad->uploadhandler. Any idea why I cant access those properties?
As inheritance chain are ok, you can access property of My_Controller from your library but not the value of the Current controller, because these two are different object.
So here is my answer how can we access the value? One way I have already mentioned. That will be enough if you need to share some property with the library. But what if you need to access all the Controller instance. There is a function to get the reference of controller instance get_instance(). You can use this function anywhere and get access of all public property controller. If you need to access any private property of controller the define a geter function to access that.
Explanation
First of all you need to learn basic about OOP. Learn about Class, Object, Inheritance..
When I said property of My_controller is different from the same property the you accessed from uploadhandler, it may confused you if you are not familiar with class and object. Here is two instance(object) of different class.
For short let say you have some classes like: Vehicle, Car, Machine and Person. All they have common attributes say name, weight ..
So, can we just inherit Any of these class from other??
Simple answer is no. We can't(!) define a Person class extending from Others. So how can we decide which incoherence would legal. If you can say Foo is a Bar you can write Foo class extending from Bar. Now from your case, It is obvious uploadhandler is not a controller. So Never Extend a anything from something that is not something.
NB: The answer is generic. If you need any specific clarification, just ask, I can update my answer

instantiate different class based on the object type in PHP

I am searching for a architectural solution in instantiating different child classes based on an object type or extending Base class with the methods of child classes.
To give an example:
There is a base class User and several child classes Partner, Client, Moderator which have specific methods an their own constructors.
When I am calling
$user = new User($userid);
I want User class
class User
{
public function __construct($userid) {
self::initDB();
if ($this->isPartner()) {
//extend this class with the methods of "Partner" child class and run "Partner" class constructor
}
if ($this->isClient()) {
//extend this class with the methods of "Client" child class and run "Client" class constructor
}
if ($this->isModerator()) {
//extend this class with the methods of "Moderator" child class and run "Moderator" class constructor
}
}
}
To return me an object with all of the methods depending on what roles does user have.
I know my logic is broken somewhere and the example I provided is wrong. But the only solution I see now is to build one giant class that has all of the methods for all of the roles - which looks like a mess.
First of all, your database logic should be totally separate from your domain objects (User, etc). Otherwise you are violating the single responsibility principle (SRP).
Set up your classes something like the following (base class User and multiple subclasses):
class User
{
private $id;
// getters and setters go here
}
class Moderator extends User {}
class Partner extends User {}
// etc
Then, create some sort of UserManager class that implements an interface that looks like the following:
interface UserManagerInterface {
function loadUserById($id);
}
The implementation of that method should load the passed user id's information from the database, look at what type it is (partner, moderator, etc) and then instantiate the appropriate class and hydrate the appropriate information.
The problem is that you cannot call new User and get anything other than a User object.
This sounds like the perfect use-case for the factory pattern.
The simplest form of this uses a static method to invoke the correct constructor.
So you could have code like this:
class User {
public static function create($userid) {
// get user from the database
// set $isPartner to true or false
// set $isClient to true or false
// set $isModerator to true or false
if ($isPartner) {
return new Partner($userid);
} elseif ($isClient) {
return new Client($userid);
} elseif ($isModerator) {
return new Moderator($userid);
} else {
return new User($userid);
}
}
}
You can then call User::create($userid) to get the appropriate object.
If your code is appropriately structured, it may well be possible to have code along the lines of Lusitanian's answer (fleshed out) that would do a better, more flexible job.

Categories