Is the only way to assign $systime a value of a built-in-functions, is through a method?
class Test{
private $systime;
public function get_systime(){
$this->systime = time();
}
}
Right off i would think something like this right?:
class Test{
private $systime = time();
public function get_systime(){
echo $this->systime;
}
}
Thanks
You should be able to use a constructor to assign the value, for example:
class Test {
private $systime;
function __construct() {
$this->systime = time();
}
public function get_systime(){
echo $this->systime;
}
}
$t = new Test();
$t->get_systime();
For more information on __construct() see the php manual section on object oriented php.
From http://www.php.net/manual/en/language.oop5.basic.php (Just before Example 3)
The default value must be a constant
expression, not (for example) a
variable, a class member or a function
call.
However, you can also assign a value from the constructor:
class Test{
private $systime;
public function __construct(){
$this->systime = time();
}
public function get_systime(){
echo $this->systime;
}
}
Related
I'm trying to increase the value of a variable which is declared in an abstract class, but every time I increase it from the child classes it keeps assigning 1 instead of increasing its value in every instance.
<?php
abstract class sum {
private $sumResult = 0;
}
class test1 extends sum {
private $sumResult;
public function __construct() {
$this->setSumResult();
}
public function setSumResult() {
$this->sumResult++; //here I try to increase the value of the variable
}
public function getSumResult() {
return $this->sumResult;
}
}
class test2 extends sum {
private $sumResult;
public function __construct() {
$this->setSumResult();
}
public function setSumResult() {
$this->sumResult++; //here I try to increase the value of the variable
}
public function getSumResult() {
return $this->sumResult;
}
}
$test1 = new test1();
$test2 = new test2();
echo $test2->getSumResult(); //Here it prints out 1 instead of 2.
?>
So, what I want is to increase the value of the variable sumResult in every instance, but it is not working. In the code I wrote it should return 2 instead of 1.
What am I doing wrong?
Thank you in advance
Classes provide a blueprint for the construction of instances. So, each instance has its own set of member variables. Changing a member variable in one instance doesn't change its value for another instance.
Since there's no rule without exception, there are static methods and members. These belong to the class, not the instance.
To share a variable across all instances of the classes, you will need to declare it as static and here I use protected to allow the derived classes to access the same value. Then each reference to this variable is done using self::$sumResult. You also don't need to declare the variable in each class as this can hide the field in the parent class.
abstract class sum {
protected static $sumResult = 0;
}
class test1 extends sum {
public function __construct() {
$this->setSumResult();
}
public function setSumResult() {
self::$sumResult++;
}
public function getSumResult() {
return self::$sumResult;
}
}
class test2 extends sum {
public function __construct() {
$this->setSumResult();
}
public function setSumResult() {
self::$sumResult++;
}
public function getSumResult() {
return self::$sumResult;
}
}
$test1 = new test1();
$test2 = new test2();
echo $test2->getSumResult(); //Here it prints out 2.
Just an alternative version to what others posted, if that can help. More logical this way imho, considering how static methods/properties work:
<?php
abstract class sum
{
private static $sumResult = 0;
public function __construct()
{
self::setSumResult();
}
public static function getSumResult()
{
return self::$sumResult;
}
private static function setSumResult()
{
self::$sumResult++;
}
}
class test1 extends sum {}
class test2 extends sum {}
$test1 = new test1();
$test2 = new test2();
echo sum::getSumResult();
I have a simple example of code in which i would like to set the private property $ttlBal.
<$php
$balance = new Customer;
$greeting = fopen("greeting.txt", "r");
while(!feof($greeting)){
echo fgets($greeting) . "<br>";
}
fclose($greeting);
$balance->acctBal = 12;
$balance->deposits = 12;
$balance->fdr = 12;
$balance->findAvail($balance->acctBal, $balance->deposits, $balance->ttlBal);
class Customer{
public $acctBal;
public $deposits;
private $acctAvail;
private $ttlBal;
public $fdr;
public function findAvail($bal, $dep, $ttlBal){
echo $this->ttlBal = $bal - $dep;
}
}
?>
This brings about an error that I cannot access the private property $ttlBal. In which way can I access this.
You should add a public setter method to your class:
class Foo {
private $var;
public function setVar($value) {
$this->var = $value;
}
}
Also in many cases protected is what you want if you use private. If you just want to hide the variable from public access, use protected.
Youe error is here $balance->ttlBal
Either you will make the property public or you would implement get() and set() methods for it in Customer class.
As an example
public function get_ttlBal()
{
return $this->ttlBal;
}
and then you can call
$balance->findAvail($balance->acctBal, $balance->deposits, $balance->get_ttlBal());
To access private properties, previously set by setter function, you should write and use getter method.
public function getVar() {
return $this->_var;
}
I have a class:
class My_Class {
private $playlist_table_name;
public function __construct() {
$this->playlist_table_name = "something";
require_once('markup.php');
}
}
How do I access $playlist_table_name from markup.php file?
I tried using: $this->playlist_table_name, but I get:
Using $this when not in object context
If you want to access the variable like that, you will need to mark it as public
class My_Class {
public $playlist_table_name;
public function __construct() {
$this->playlist_table_name = "something";
require_once('markup.php');
}
}
You are then going to want to instantiate the class before attempting to use it.
$MyClass = new My_Class;
echo $MyClass->playlist_table_name;
That will allow you to echo out the value.
I am trying to get a variable from a php class without having to use "new classname()"
This is my code:
class myVars {
static $varx = null;
public function __construct() {
$this->varx = "test";
}
}
echo myVars::$varx;
I also tried replacing $this-> with self::, but nothing gets printed. How should I code the class in order to call myVars::$varx?
The problem you are having, is that you are not instantiating an object, so the constructor never gets called.
What you could do is:
class myVars {
static $varx = null;
public function __construct() {
$this->varx = "test";
}
}
myVars::$varx = "test";
echo myVars::$varx;
Or you could create an object and have the constructor change the static variable.
This should make your static variable publicly accessible.
class myVars {
public static $varx = null;
public function __construct() {
self::$varx = "test";
}
}
echo myVars::$varx;
However, in your example, the constructor is never called, so the modification to the static variable is never made.
I have a global variable outside my class = $MyNumber;
How do I declare this as a property in myClass?
For every method in my class, this is what I do:
class myClass() {
private function foo() {
$privateNumber = $GLOBALS['MyNumber'];
}
}
I want this
class myClass() {
//What goes here?
var $classNumber = ???//the global $MyNumber;
private function foo() {
$privateNumber = $this->classNumber;
}
}
EDIT: I want to create a variable based on the global $MyNumber but
modified before using it in the methods
something like: var $classNumber = global $MyNumber + 100;
You probably don't really want to be doing this, as it's going to be a nightmare to debug, but it seems to be possible. The key is the part where you assign by reference in the constructor.
$GLOBALS = array(
'MyNumber' => 1
);
class Foo {
protected $glob;
public function __construct() {
global $GLOBALS;
$this->glob =& $GLOBALS;
}
public function getGlob() {
return $this->glob['MyNumber'];
}
}
$f = new Foo;
echo $f->getGlob() . "\n";
$GLOBALS['MyNumber'] = 2;
echo $f->getGlob() . "\n";
The output will be
1
2
which indicates that it's being assigned by reference, not value.
As I said, it will be a nightmare to debug, so you really shouldn't do this. Have a read through the wikipedia article on encapsulation; basically, your object should ideally manage its own data and the methods in which that data is modified; even public properties are generally, IMHO, a bad idea.
Try to avoid globals, instead you can use something like this
class myClass() {
private $myNumber;
public function setNumber($number) {
$this->myNumber = $number;
}
}
Now you can call
$class = new myClass();
$class->setNumber('1234');
Simply use the global keyword.
e.g.:
class myClass() {
private function foo() {
global $MyNumber;
...
$MyNumber will then become accessible (and indeed modifyable) within that method.
However, the use of globals is often frowned upon (they can give off a bad code smell), so you might want to consider using a singleton class to store anything of this nature. (Then again, without knowing more about what you're trying to achieve this might be a very bad idea - a define could well be more useful.)
What I've experienced is that you can't assign your global variable to a class variable directly.
class myClass() {
public $var = $GLOBALS['variable'];
public function func() {
var_dump($this->var);
}
}
With the code right above, you get an error saying "Parse error: syntax error, unexpected '$GLOBALS'"
But if we do something like this,
class myClass() {
public $var = array();
public function __construct() {
$this->var = $GLOBALS['variable'];
}
public function func() {
var_dump($this->var);
}
}
Our code will work fine.
Where we assign a global variable to a class variable must be inside a function. And I've used constructor function for this.
So, you can access your global variable inside the every function of a class just using $this->var;
What about using constructor?
class myClass {
$myNumber = NULL;
public function __construct() {
global myNumber;
$this->myNumber = &myNumber;
}
public function foo() {
echo $this->myNumber;
}
}
Or much better this way (passing the global variable as parameter when inicializin the object - read only)
class myClass {
$myNumber = NULL;
public function __construct($myNumber) {
$this->myNumber = $myNumber;
}
public function foo() {
echo $this->myNumber;
}
}
$instance = new myClass($myNumber);
If you want to access a property from inside a class you should:
private $classNumber = 8;
I found that globals can be used as follows:
Create new class:
class globalObj{
public function glob(){
global $MyNumber;
return $this;
}
}
So, now the global is an object and can be used in the same way:
$this->glob();
class myClass
{
protected $foo;
public function __construct(&$var)
{
$this->foo = &$var;
}
public function foo()
{
return ++$this->foo;
}
}