Expose "Use" Classes to Included File

Expose use classes to Included file

Simply: no. See note bellow the example http://www.php.net/manual/en/language.namespaces.importing.php#example-247

Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.

Regarding C++ Include another class

What is the basic problem in your code?

Your code needs to be separated out in to interfaces(.h) and Implementations(.cpp).

The compiler needs to see the composition of a type when you write something like

ClassTwo obj;

This is because the compiler needs to reserve enough memory for object of type ClassTwo to do so it needs to see the definition of ClassTwo. The most common way to do this in C++ is to split your code in to header files and source files.

The class definitions go in the header file while the implementation of the class goes in to source files. This way one can easily include header files in to other source files which need to see the definition of class who's object they create.

Why can't I simply put all code in cpp files and include them in other files?

You cannot simple put all the code in source file and then include that source file in other files.C++ standard mandates that you can declare a entity as many times as you need but you can define it only once(One Definition Rule(ODR)). Including the source file would violate the ODR because a copy of the entity is created in every translation unit where the file is included.

How to solve this particular problem?

Your code should be organized as follows:

//File1.h

Define ClassOne 

//File2.h

#include <iostream>
#include <string>

class ClassTwo
{
private:
string myType;
public:
void setType(string);
std::string getType();
};

//File1.cpp

#include"File1.h"

Implementation of ClassOne

//File2.cpp

#include"File2.h"

void ClassTwo::setType(std::string sType)
{
myType = sType;
}

void ClassTwo::getType(float fVal)
{
return myType;
}

//main.cpp

#include <iostream>
#include <string>
#include "file1.h"
#include "file2.h"
using namespace std;

int main()
{

ClassOne cone;
ClassTwo ctwo;

//some codes
}

Is there any alternative means rather than including header files?

If your code only needs to create pointers and not actual objects you might as well use Forward Declarations but note that using forward declarations adds some restrictions on how that type can be used because compiler sees that type as an Incomplete type.

use keyword scope in included file

The documentation on using namespaces states:

Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.

This means that you can't "prepare" a child script with aliases for the classes you want it to access using only their base names. Each script must either define their own aliases (within the same file) or always use canonical class names.

Can I include code into a PHP class?

No. You cannot include files in the class body.

In a file defining a class, you may only include files in a method body or outside the class body.

From your description I take you want this:

<?php // MyClass.php
class MyClass
{
protected $_prop;
include 'myclass-methods.php';
}

<?php // myclass-methods.php
public function myMethod()
{
$this->$_prop = 1;
}

Running this code will result in

Parse error: syntax error, unexpected T_INCLUDE, expecting T_FUNCTION

What is possible though is this

<?php // MyClass.php
class MyClass
{
protected $_prop;
public function __construct() // or any other method
{
include 'some-functions.php';
foo($b); // echoes 'a';
}
}

<?php // some-functions.php
$b = 'a';
function foo($str)
{
echo $str;
}

Doing it this way, will import the contents of the include file into the method scope, not the class scope. You may include functions and variables in the include file, but not methods. You could but should not put entire scripts into it as well and change what the method does, e.g.

<?php // MyClass.php
// ...
public function __construct($someCondition)
{
// No No Code here
include ($someCondition === 'whatever') ? 'whatever.php' : 'default.php';
}
// ...

<?php // whatever.php
echo 'whatever';

<?php // default.php
echo 'foo';

However, patching the class this way to exhibit different behavior is not how you should do it in OOP. It's just plain wrong and should make your eyes bleed.

Since you want to dynamically change behavior, extending the class is also not a good option (see below why). What you really will want to do is write an interface and make your class use objects implementing this interface, thus making sure the appropriate methods are available. This is called a Strategy Pattern and works like this:

<?php // Meowing.php 
interface Meowing
{
public function meow();
}

Now you got the contract that all Meowing Behaviors must obey, namely having a meow method. Next define a Meowing Behavior:

<?php // RegularMeow.php
class RegularMeow implements Meowing
{
public function meow()
{
return 'meow';
}
}

Now to use it, use:

<?php // Cat.php
class Cat
{
protected $_meowing;

public function setMeowing(Meowing $meowing)
{
$this->_meowing = $meowing;
}

public function meow()
{
$this->_meowing->meow()
}
}

By adding the Meowing TypeHint to setMeowing, you make sure that the passed param implements the Meowing interface. Let's define another Meowing Behavior:

<?php // LolkatMeow.php
class LolkatMeow implements Meowing
{
public function meow()
{
return 'lolz xD';
}
}

Now, you can easily interchange behaviors like this:

<?php
require_once 'Meowing.php';
require_once 'RegularMeow.php';
require_once 'LolkatMeow.php';
require_once 'Cat.php';

$cat = new Cat;
$cat->setMeowing(new RegularMeow);
echo $cat->meow; // outputs 'meow';
// now to change the behavior
$cat->setMeowing(new LolkatMeow);
echo $cat->meow; // outputs 'lolz xD';

While you also could have solved the above with inheritance by defining an abstract BaseCat and meow method and then deriving concrete RegularCat and Lolkat classes from that, you have to consider what you want to achieve. If your cats will never change the way they meow, go ahead and use inheritance, but if your RegularCat and Lolkat is supposed to be able to do arbitrary meows, then use the Strategy pattern.

For more design patterns in PHP, check these resources:

  • http://www.php.net/manual/en/language.oop5.patterns.php
  • http://www.ibm.com/developerworks/library/os-php-designptrns/
  • http://www.fluffycat.com/PHP-Design-Patterns/
  • http://sourcemaking.com/design_patterns

How to use class from other files in C# with visual studio?

According to your explanation you haven't included your Class2.cs in your project. You have just created the required Class file but haven't included that in the project.

The Class2.cs was created with [File] -> [New] -> [File] -> [C# class] and saved in the same folder where program.cs lives.

Do the following to overcome this,

Simply Right click on your project then -> [Add] - > [Existing Item...] : Select Class2.cs and press OK

Problem should be solved now.

Furthermore, when adding new classes use this procedure,

Right click on project -> [Add] -> Select Required Item (ex - A class, Form etc.)

Including JavaScript class definition from another file in Node.js

You can simply do this:

user.js

class User {
//...
}

module.exports = User // Export class

server.js

const User = require('./user.js')

let user = new User()

This is called CommonJS module.

ES Modules

Since Node.js version 14 it's possible to use ES Modules with CommonJS. Read more about it in the ESM documentation.

user.mjs ( extension is important)

export default class User {}

server.mjs

import User from './user.mjs'

let user = new User()

PHP class inherit variables in included file

As explained in include() and variable scopes, when you include a file in your __construct() method, the scope of the variables in the file you're including is limited to the __construct() method, not the class.

Your options would be to either change the content of the included file to include a $this-> in front of the variable name (i.e. $this->var_in_included_file = 10;) or add a $this->var_in_included_file = $var_in_included_file; in your __construct() method.

Importing class from another file

Your problem is basically that you never specified the right path to the file.

Try instead, from your main script:

from folder.file import Klasa

Or, with from folder import file:

from folder import file
k = file.Klasa()

Or again:

import folder.file as myModule
k = myModule.Klasa()


Related Topics



Leave a reply



Submit