I have a static class that loads additional php file inside of one of its function and I need to access class variables from this file withous knowing the class name.
But This::SomeVar - doesn't work.
But I know there's another way to do it, I just can't find anything about it.
So here's the example class
class SomeClass {
static function Initialize() {
require_once 'somefile.php';
}
}
and inside that file I need to access static variable something like this
This::SomeVar= 'qwe';
Use self::$SomeVar to access static class members.
$this->someVar for fields and self::$someVar for statics
You can use $this->someVar to access a property from inside a class.
Related
Inside a method in my Model class, I include another PHP file. The code works until that included PHP file declares a class definition ie class Test123 {}.
The class name is unique. The only possible source of the issue that I could think of would be how I'm including a class within a class. However, I wasn't able to find information about this, so I assume it isn't a problem.
Any ideas?
//Inside of method inside of model class
include "{$_SERVER['DOCUMENT_ROOT']}/models/language.php";
I believe you're right in assuming that you can't declare a class inside a class, which is what you're essentially doing by including it inside a class method.
Could you perhaps do something like this?
class.php:
<?php
include("otherclass.php");
class MyClass {
public function run () {
$otherClass = new OtherClass();
}
}
otherclass.php:
<?php
class OtherClass {
}
index.php:
<?php
include("class.php");
$myClass = new MyClass();
$myClass->run();
Let's say I have a class:
class test {
public static function sayHi() {
echo 'hi';
}
}
Call it by test::sayHi();
Can I define sayHi() outside of the class or perhaps get rid of the class altogether?
public static function sayHi() {
echo 'hi';
}
I only need this function to encapsulate code for a script.
Maybe the answer is not a static method but a plain function def?
A static method without a class does not make any sense at all. The static keywords signals that this method is identical for all instances of the class. This enables you to call it.. well.. statically on the class itself instead of on one of its instances.
If the content of the method is self-contained, meaning it does not need any other static variables or methods of the class, you can simply omit the class and put the code in a global method. Using global methods is considered a bad practice.
So my advice is to just keep the class, even if it has only that one method within. This way you can still autoload the file instead of having to require it yourself.
Functions in OOP are public by default, you can modify them to private like:
private function test(){
//do something
}
Or like you said, to static, in public or private like:
private static function test(){
//do something
}
But if you're not using OOP, functions by default are global and public, you can't change their access to private. That's not the way they are supposed to works because if you change their type to private, you will NEVER be able to access to that function. Also, static doesn't works because is another property of OOP...
Then, you can simply create that function and access it from everywhere you want (obviously where is available :P because you need to include the file where is stored)
This is a PHP newbie question:
I want to give my class access to my database credentials in an include file: ../config.inc
<?php
$db_info['host']='localhost'; // and so forth
...
?>
Later, in my class source file I have:
<?php
require_once('../config.inc'); // include the above file
public class Foo {
static function Host() {
echo $db_info['host'];
}
}
?>
When try to access the class in other code, I get an error claiming that $db_info is undefined. When I try to move the require_once inside the class scope (after Foo {) I get a syntax error, so apparently one can't use require_once inside the class. What are the best practices when writing class static methods that need access to included data? THANKS.
You have a issue with the scope. Your included variable is available outside your class but not inside your class. The suggested methods should be to pass the variable to the constructor of your class and store it in a member variable of the class.
But since your using the function as static then you can use global but its not best practices to do so. alternatively you can include the file with in the function.
public class Foo {
static function Host() {
require_once('../config.inc'); // include the above fil
echo $db_info['host'];
}
}
I have like:
class Class2 extends Class1 {
.....
function __construct() {
parent::__construct();
$var_in_included_file;
}
}
class Class1 {
function __construct() {
include_once('my_file.php')
}
.....
}
my_file.php:
$var_in_included_file=10;
The problem is that I cannot receive value of $var_in_included_file. Is there a way to receive this value without add many codice like:
$this->var=$var_in_included_file ....?
Because I have many thousands of variables.
Thanks.
More abstract the problem is:
in some file I received (from $_POST) near 500 variable.
These variables have be elaborated in complicated way. For simplify this elaborating I need create tree of class inheritans - but in this case these variables will not seen in child classes without assigning them to class variables - but this produses enormous volume of code.
In class one, assign your variables to class variables:
class Class1{
private $someVariable;
public function __construct(){
include_once 'my_file.php';
// variable declared in my_file.php
$this->someVariable = $someVariable;
}
}
Now that variable is accessible in child class, through $this->someVariable.
Happy coding, good-luck
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.
Class1 {
include_once('my_file.php')
.....
}
Is not possible
I currently have a error-system like this on my header.php:
include('class.error.php');
Errsys::disable_default();
Errsys::enable_logging('errors.dat');
So, I'm not creating a new object via $asd = new Errsys;. How to add a variable to class, so it can be called like Errsys::variable or via any similar syntax inside or outside the class?
Hope you understood.
Martti Laine
Which version of php are you using? You can add a class variable like:
class className {
public $varname;
............ more code
In static context:
class className {
public static $varname;
............ more code
You can create a static variable similar to creating a static function:
public static $whatever;
I would recommend reading PHP manual's section on classes and objects for further information.
ps. If your class is called Errsys, I'd recommend calling the file class.errsys.php as well instead of class.error.php :)