How to add a public var in class in php? - php

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 :)

Related

Can you define a static method outside of a class?

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)

php pass class to function

I have a class with property transof
class translator {
public function transof($phrase) { gives translation of phrase }
}
Now I want to pass an instance of translator to a function:
function parse($part,$class) {
$class->transof($part);
}
$tr = new translator("project","en");
parse("exception",$tr);
Do anyone know how to do this?
I know this example is to simple, and can be easily written without the use of a function, but in my real world example I would like to be able to use a function.
Of course I can use global $tr in the function, and use it inside the function, but I don't like using global.
Thanks in advance
If you do not want to create object in global code to have better maintainability then you need to modify the signature of the function as follows:
function parse($part,$project,$lang) {
$class = new Translator($project,$lang);
$class->transof($part);
}
parse("exception","project","en");
It does what you intend, the object is passed to the function parse. All you need to do is to include the class definition file in or before the php file which contains parse definition or get use of __autoload() function which will include class definition when needed.
You can also define parse this way:
function parse($part, translator $class) {
$class->transof($part);
}
Then code editors as Aptana etc. will know what class this object is an instance of and will be able to provide you with hints concerning your class structure.

Basics of PHP class:passing value in class

I am debugging a site
I am having to work with classes which I am not that used to as of yet.
There is this class in this site that processes a $this but there does not seem to be any variable passed to the class.
the class is like this
class myclass extends otherclass{
function dosmthtomyclass{
print_r($this);
}
}
function dosmttomyclass prints an array.
there are bunch of variables defined protected in the class but there does not seem to be any specific value specified for any of those variables and there is no constructor in the class to the pass the value to.
I am seriously confused as to where the variable must have been passed from.
This may be something really basic but any help would be appreciated.
what are the possible ways of passing variables to the class
$this refers to the current object. according to PHP documentation
The pseudo-variable $this is available when a method is called from
within an object context. $this is a reference to the calling object
(usually the object to which the method belongs, but possibly another
object, if the method is called statically from the context of a
secondary object).
here is some detailed explanation about it. which may help you to understand
What does the variable $this mean in PHP?
this is because myclass is getting the data from otherClass... like this:
<?php
class otherclass{
public $name="Mike";
}
class myclass extends otherclass {
function dosmthtomyclass() {
print_r($this);
}
}
$test=new myclass();
$test->dosmthtomyclass(); //prints "[name] => Mike"
You need to go through the manual and/or tutorials on OOP. Because this is the only way you'll understand OOP. Start with this: http://www.php.net/manual/en/language.oop5.basic.php
$this refers to the current instance of the object. Read about PHP+visibility to understand why private varianbles/methods aren't visisble to child classes (extended classes).
Good luck!
$this refers to the current object of the class. Execute the following code for more clarity:
<?php
class abc {
var $val = 3;
function show()
{
print_r($this);
}
}
$ob = new abc();
$ob->show();
Maybe some background on using classes in php can help you:
$this is used to refer to the hierarchy within the class.
E.g., you could have a class like this:
class Phpclass{
public function main(){
echo "public function called<br/>";
$this->helloworld();
}
private function helloworld(){
echo "hello world";
}
}
$phpclass=new Phpclass();
$phpclass->main();
This class is a blueprint of an object that will be instantiated with the variables $phpclass. As main() is a plublic function in the class, it can be called from outside the class. The private function can only be called from inside the class, so the main() function uses $this as identifier for the class itself to call the private function helloworld() inside itself. Without $this, the object wouldn't know that you are referring to a function inside itself.
First, the above will echo out "public function called", then "hello world".

What to use istead of THIS in php OOP?

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.

Should I place variables in class or constructor? PHP

My question(s) is one of best practices for OOP. Im using Codeigniter framework/PHP.
I have a class:
class Test() {
var $my_data = array();
function my_function() {
//do something
}
}
Is it ok to declare $my_data in the class like that? or should it go in the constructor? Basically every function will be writing to $my_data so in a sense it will be a class-wide variable(global?, not sure about the terminology)
Also, should I use var or private ? is var deprecated in favor of declaring the variables scope?
If you want $my_data to be available to all methods in Test, you must declare it at the class level.
class Test {
private $my_data1 = array(); // available throughout class
public function __construct() {
$my_data2 = array(); // available only in constructor
}
}
var is deprecated and is synonymous with public. If $my_data doesn't need to be available outside of Test, it should be declared private.
If it belongs "to the class", put it in the class. If it belongs "to an instance of the class", put it in the constructor. It kinda sounds like you should be using the session, though.
its fine if you declare the variable outside constructor.
actually codeigniter will not let you give any parameter at your constructor.
and the variable will automatically assigned value when the class is instantiated.
for default, any of php variable and function with in a class will be have a public access.
i don't really thing you need to use access modifier at codeigniter.
the library it self don't define any access modifier.

Categories