PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label extends. Show all posts
Showing posts with label extends. Show all posts

Wednesday, August 24, 2022

[FIXED] How to extend class inside module.exports in node js?

 August 24, 2022     class, extends, module, module.exports, node.js     No comments   

Issue

So, using Node.js v18.3, I don't want to write new class every time in there:

module.exports = {Class1, Class2, /*etc...*/};

That's why I using this way of doing it:

module.exports = {
  ClassName: class ClassName {
    constructor(...args) {
      // properties...
    };
  },
};

But, when I try to extend the class:

module.exports = {
  ClassName: class ClassName {
    constructor(...args) {
      // properties...
    };
  },

  AnotherClass: class AnotherClass extends ClassName {
    // it does not work, sadly...
  },
};

It gives this error:

  AnotherClass: class AnotherClass extends ClassName {
                                           ^

ReferenceError: ClassName is not defined

Tried to work around it with this.ClassName:

  AnotherClass: class AnotherClass extends this.ClassName {
    // does not work too...
  },

But it did not work either:

  AnotherClass: class AnotherClass extends this.ClassName {
                                                ^

TypeError: Class extends value undefined is not a constructor or null

First of all, why does it? Why does it not work in first time, without this.?
Second, how do I fix this or is there any other way doing this, without manually putting all classes in object, as I showed at the beginning of the question?


Solution

You’re doing a class expression rather than a class declaration, so at the time your code runs inside module.exports the classes are not defined yet, so cannot be extended from.

Instead of using a single module.exports you could use multiple exports, for example:

exports.Class1 = class Class1 {
  /* … */
};

exports.Class2 = class Class2 extends exports.Class1 {
  /* … */
};


Answered By - davecardwell
Answer Checked By - Mary Flores (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, March 8, 2022

[FIXED] How to extend cactiverecord

 March 08, 2022     extends, model, yii     No comments   

Issue

I want add aditional methods in CActiveRecord, but if class Project_Model extends CActiveRecord {} get error

The table "Project_ActiveRecord" for active record class "Project_ActiveRecord" cannot be found in the database.

I want create simple structure: CActiveRecord->Project_ActiveRecord (only extend methods)->Table (real table).

How can do this?


Solution

The error is clear: You don't have a table named Project_ActiveRecord in your DB!

If Project_Model is going to be the base model for your others active record models then you should do something like:

//A base classe example that has a behavior shared by all the inherited class
abstract class Project_Model extends CActiveRecord
{

    public function behaviors(){
        return array(
            'CTimestampBehavior' => array(
                'class' => 'zii.behaviors.CTimestampBehavior',
                'setUpdateOnCreate' => true
            ),
        );
    }

}

And then you can declare the other class that will have a table in the db:

class YourClass extends Project_Model
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Token the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'YourClassTable';
    }
    ...
}

Then you shoudl never call the class Project_Model (this is why I put the keyword abstract) in your code, you have to call the inherited classes that have an existing table in the db!



Answered By - darkheir
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing