I have a custom module, which has its own tables and it runs some queries to provide the data.

Can someone help how can I create a new class in drupal custom module?

I looked into the documentation and could not find anything helpful. If someone can provide with some link, it is highly appreciated.

Thanks

Comments

neerajsingh’s picture

You can use Database API's to fetch/insert/update your content of custom tables. A new class is not required to achieve this.

Jaypan’s picture

And to add to that, you can use hook_menu() to create paths at which database events can happen, and the Form API to create forms if necessary.

shenagarg’s picture

I have already used hook_menu and Form API to add/update database records. I would like to create a class to return object when we select database records.

shenagarg’s picture

I have coded the past to add/edit/delete the records in database.

Drupal system is synchronized with other system. Drupal returns the data from database in form of array of particular class. So, I need to create the class for it and not to add/update/delete records from database.

Jaypan’s picture

I don't get what your question is. If you want to declare a new class, you can declare it:

class Something [extends someOtherClass] {
  // code goes here
}

What is the problem you are having?

shenagarg’s picture

There is no problem. I just want to know if I should define class in Drupal as we do for Core PHP OR there are some other practices recommended by Drupal.

Jaypan’s picture

I see.

Well, the only real question is whether you want to define your class as an entity type or not. If you do, you'll want to extend DrupalDefaultEntityController, and implement hook_entity_info(). Otherwise you can just declare it as a regular class.

shenagarg’s picture

I want to define my class as an entity type. Is there some documentation related to DrupalDefaultEntityController to define classes? If yes, could you please let me know the link(s).

Many thanks.

Jaypan’s picture

nitin.k’s picture

Hi,

First of all whatever class you are using , you need to decide the scope of functionality whether you want in the whole application as re usability or just inside your module. Because in Drupal you can access any function anywhere. So it is very important to decide the scope of your classes and functions.

It will be useful to use spl_autoload_register() function for calling classes and methods without including files and try to use name spaces as well for better code.
http://php.net/manual/en/function.spl-autoload-register.php

shenagarg’s picture

Nitin

In my module, I have few functions which return array of classes. Any other module can call these functions. Scope of class and function is to whole application and not only limited to the module.