getall function in PHP - php

What is wrong in my class.It;s giving me syntax error, unexpected ',' on my getall function.Is is not possible to send multiple return value? What would be the problem.
class Form
{
private $name;
private $email;
private $pass;
private $rpass;
private $phone;
public static function setname($name)//setting name
{
$this->name=$name;
}
public static function email($email)//setting email
{
$this->email=$email;
}
public static function password($pass)//setting password
{
$this->pass=$pass;
}
public static function repassword($rpass)//password again
{
$this->rpass=$rpass;
}
public static function phone($phone)
{
$this->phone=$phone;
}
public static function getall() //getting all value
{
$a=$this->name;
$b=$this->email;
$c=$this->pass;
$d=$this->rpass;
$e=$this->phone;
return($a,$b,$c,$d,$e);//here is the problem
}
}

It seems like you want to return an array, but what you have is invalid syntax it doesn't mean anything.
So change this:
return($a,$b,$c,$d,$e);
to this:
return [$a,$b,$c,$d,$e];
//^ See here ^
For more information about arrays see the manual: http://php.net/manual/en/language.types.array.php#language.types.array.syntax
Also you can't have static functions with $this. Because $this is only accessible in object syntax, but not in the class itself, so I think you want to remove the static keyword from the functions.

Related

public static function php pass variable

I need to pass a variable from a static function to another within the same class.
I do not write the full code, I need the theoretical procedure
enter code here
class One
{
public static function One()
{
/**
* some code extract from DB $one
*/
}
public static function two()
{
/**
* I need to retrieve the variable $one to use it in another query DB
*/
}
}
Note:
you can't use $this in a static function
Declare $one as a static variable:
private static $one;
And you can access it using : self::$one
You need to declare your variable within your One class, then you can retrieve it using self and the scope resolution operator ::.
class One {
private static $one;
public static function One() {
self::$one = something_from_your_db();
}
public static function two() {
do_something(self::$one);
}
}

PHP Class throws an error, what is wrong

I have the class.
Class User {
private $_name;
private $_email;
public static function factory() {
return new __CLASS__;
}
public function test() {
}
}
and when i make a static method call using the syntax below.
User::factory();
it throws me following syntax error.
Parse error: syntax error, unexpected T_CLASS_C in htdocs/test/index.php on line 8
the error is being thrown because the Static factory() method is unable to create the object during the static method call.
and when i change the magic constant __CLASSS__ to the name of the current class i.e to User then it works.
what am i missing?
Try:
Class User {
private $_name;
private $_email;
public static function factory() {
$class = __CLASS__;
return new $class;
}
public function test() {
}
}
Not really sure why your example doesn't works. But what does work is:
public static function factory()
{
return new self();
}
Try this:
$class = __CLASS__;
return new $class;
Why don't you return self or $this?
Check out the singleton patterns: http://www.phpbar.de/w/Singleton and http://php.net/manual/language.oop5.patterns.php
Other solution would be
return clone $this;

php Abstracted static variables persist between static method calls

I have a php abstract generic class with several static variables and functions.
class Generic{
public static $ID;
private static $tableName;
private static $oldTableName;
protected static $DAL;
public static function Init($tableName, $id=null) {
if(self::$tableName)
self::$oldTableName=self::$tableName;
self::$tableName=$tableName;
self::$DAL = new DAL(self::$tableName);
}
public static function RecallTableName(){
if(self::$oldTableName){
self::$tableName=self::$oldTableName;
self::$oldTableName=null;
return true;
}
}
public static function GetProperty($id, $columnName, $whereStatement = "1=1"){
if(!self::$tableName)
return false;
//DO DB STUFF HERE
return; //RETURN WHAT DB CALLS DID HERE
}
}
Several classes inherit from this generic class using extends.
class Part extends Generic {
static function Init($tableName="part",$id = null) {
$return = parent::Init($tableName,$id);
new Part();
return $return;
}
public static function destruct(){
parent::RecallTableName();
}
public function __destruct() {
Part::destruct();
}
public static function GetProperty($id, $columnName, $whereStatement = "1=1") {
self::Init();
return parent::GetProperty($id, $columnName, $whereStatement);
}
}
class System extends Generic {
static function Init($tableName="system",$id = null) {
$return = parent::Init($tableName,$id);
new System();
return $return;
}
public static function destruct(){
parent::RecallTableName();
}
public function __destruct() {
Part::destruct();
}
public static function GetProperty($id, $columnName, $whereStatement = "1=1") {
self::Init();
return parent::GetProperty($id, $columnName, $whereStatement);
}
public static function GetInventory($PartManageIDToCheck)
{
return Part::GetInventory($PartManageIDToCheck);
}
}
This way when I implement a method call on a child I can hook on the child's death. Going further, I can call:
System::GetInventory(1)
This will:
1 - Call Init() on Generic which stores "system" in the Generic::$tableName.
2 - Cenerate a nested sibling (Part) which in turn calls Init() on Generic which then moves Generic::$tableNameto Generic::$oldTableNameand stores "part in Generic::$tableName
This all works without issue. However, when I use two children back to back it falls apart.
System::GetProperty(1, "ID");
Part::GetProperty(3, "ID");
Using the two back to back allows Generic to persist somehow so that Generic::$tableName == "system" when Part::GetProperty(3, "ID"); gets called.
Any idea how to fix this?
What you're looking for is late static binding which was added in PHP 5.3. Change the definition of $tableName to protected static $tableName. Then redeclare it exactly the same way in each child class.

Initialize static member with custom class in php

as there are no enums in PHP I tried to do something like this:
class CacheMode{
public static $NO_CACHE = new CacheMode(1, "No cache");
private $id, $title;
public function getId(){
return $this->id;
}
public function getTitle(){
return $this->title;
}
private function __construct($id, $title){
$this->id = $id;
$this->title = $title;
}
}
The problem is, that I get a parse error if I run the script:
Parse error: syntax error, unexpected T_NEW
I "worked it aroud" with this:
class CacheMode{
public static function NO_CACHE(){
return new CacheMode(1, __("No cache",'footballStandings'));
}
public static function FILE_CACHE(){
return new CacheMode(2, __("Filecache",'footballStandings'));
}
public static function VALUES(){
return array(self::NO_CACHE(), self::FILE_CACHE());
}
private $id, $title;
public function getId(){
return $this->id;
}
public function getTitle(){
return $this->title;
}
private function __construct($id, $title){
$this->id = $id;
$this->title = $title;
}
}
It works, but I am not really happy with it.
Can anyone explain, why I can't do the static $xyz = new XYZ(); way or has a better solution for this problem?
Its annoying, I know. I solve it like
class Foo {
public static $var;
}
Foo::$var = new BarClass;
Its a little bit similar to javas "static code blocks" (or whatever they are called ^^)
The file is only includeable once anyway (because a "class already define" error occurs), so you can be sure, that also the code below the class is executed once.
As an optimization, you could store the object instance as a static field, so that you are not creating a new object every time the static method is called:
private static $noCache;
public static function NO_CACHE(){
if (self::$noCache == null){
self::$noCache = new CacheMode(1, __("No cache",'footballStandings'));
}
return self::$noCache;
}
But yes, it is annoying that you cannot assign a new object instance to a class field when you first define the field. :(
Quoting the manual page of static :
Like any other PHP static variable,
static properties may only be
initialized using a literal or
constant; expressions are not allowed.
So while you may initialize a static
property to an integer or array (for
instance), you may not initialize it
to another variable, to a function
return value, or to an object.
That is why you cannot do
public static $NO_CACHE = new CacheMode(1, "No cache");

How to override a static property of a parent object and let the parent object access the new value in PHP?

This is what I have: All objects that can be persisted on the database extend the DatabaseObject abstract class, which has all the logic code to actually watch for attribute changes and run the databas queries.
I'm using two static variables to define object-specific details. I define them generically in the base class, and then supposedly I overwrite them in the actual database objects.
The problem is: When the code in the parent class is actually executed, it uses the old parent value instead of the current object value.
Here's the code for the base class:
abstract class DatabaseObject {
public $id;
private static $databaseTable = NULL;
private static $databaseFields = array();
private $data = array();
private $changedFields = array();
public function IDatabaseObject($id) {
$this->id = $id;
$this->data = Database::GetSingle(self::$databaseTable, $id);
Utils::copyToObject($this, $this->data, self::$databaseFields);
}
public static function Load($id) {
return new self($userID);
}
public static function Create($data) {
$id = Database::Insert(self::$databaseTable, $data);
return new self($id);
}
public function Save() {
$data = Utils::copyFromObject($this, $this->changedFields);
Database::Update(self::$databaseTable, $data, $this->id);
}
public function __constructor() {
// We do this to allow __get and __set to be called on public vars
foreach(self::$databaseFields as $field) {
unset($this->$field);
}
}
public function __get($variableName) {
return $this->$variableName;
}
public function __set($variableName, $variableValue) {
// We only want to update what has been changed
if(!in_array($variableName, $this->changedFields) && in_array($variableName, self::$databaseFields)) {
array_push($this->changedFields, $variableName);
}
$this->$variableName = $variableValue;
}
}
And here's the code for one of the objects extending the base class above:
class Client extends DatabaseObject {
public static $databaseTable = "clients";
public static $databaseFields = array("name","contactName","primaryUserID","email","is_active","rg","cpf","cnpj","ie","addrType","addrName","addrNumber","addrComplement","addrPostalCode","addrNeighborhood","addrCity","addrState","addrCountry","phoneLandline","phoneFax","phoneMobile");
public $name;
public $contactName;
public $primaryUserID;
public $email;
public $is_active;
public $rg;
public $cpf;
public $cnpj;
public $ie;
public $addrType;
public $addrName;
public $addrNumber;
public $addrComplement;
public $addrPostalCode;
public $addrNeighborhood;
public $addrCity;
public $addrState;
public $addrCountry;
public $phoneLandline;
public $phoneFax;
public $phoneMobile;
public static function Load($id) {
return new Client($id);
}
}
What am I doing wrong here? Is there another way I can achieve the same result?
A brief addendum: I declare the attributes in the class body mainly to let it be seen by the NetBeans' auto-complete feature.
You are looking for Late Static Binding.
So you need to use:
static::$databaseTable
instead of
self::$databaseTable
This feature is available as of PHP 5.3. Simulating this in PHP 5.2 is very hard, because of two reasons: get_called_class is available only since PHP 5.3, too. Therefore it must be simulated, too, using debug_backtrace. The second problem is, that if you have the called class, you still may not use $calledClass::$property because this is a PHP 5.3 feature, too. Here you need to use eval or Reflection. So I do hope that you have PHP 5.3 ;)

Categories