In my model, I am extending Zend_Db_Table_Abstract. Inside, I have a protected $_name = 'table_name'.
Now, if I define a constructor, and a private variable private $_var and define it inside the constructor, the model does not work anymore! When I call $this->createRow() or anything, nothing happens! Why is this constructor doing this?!
This is what I have:
<?php
class myClass extends Zend_Db_Table_Abstract
{
protected $_name = 'table_name';
private $_var;
public function __construct($var)
{
$this->_var = $var;
}
public function getById($id)
{
$select = $this->select()->where('id =?',$id);
return $this->fetchRow($select);
}
}
This doesn't work! If I remove the __construct(), and the private variable, then it works! Why?
Thanks
If you override the constructor of Zend_Db_Table_Abstract, you should probably call the parent constructor:
class MyClass extends Zend_Db_Table_Abstract
{
protected $_name = 'table_name';
private $_var;
public function __construct($var)
{
$this->_var = $var;
parent::__construct(); // add this
}
// the rest...
}
The parent constructor calls some protected methods _setup() (which in turn calls _setupDatabaseAdapter() and _setupTableName()) and init() (which is empty in the parent, but you can use to add some processing to the final step of object instantiation).
you have to be more clear when asking questions. write code if you have to.. No one can determine whats wrong with your application with only 2 piece of information.. with that said..
first yo should define your variable outside constructor and initialize inside the constructor if you have to.
Your model will not magically know that a db abstract class exist which connects to your database. you will have to create an object inside the model constructor.
here is the code
// DB Abstract class
class Application_Model_DbTable_myTable extends Zend_Db_Table_Abstract
{
protected $_name = 'myTable';
}
// Your model class
class Application_Model_myModel {
private $_var;
public function __construct() {
$this->_var = new Application_Model_DbTable_myTable();
}
}
Hope this was what you were looking for.. and only thing I understood from your question.
dins
It has been implied in other answers but not stated... You class failed because you overrode the constructor and didn't call the parent constructor so the object did not get created properly.
Normally when these classes are constructed, they are constructed through setOptions() which takes an array in constructor. So if you really have to override the constructor you might try something like:
public function __construct($var, $config = array())
{
$this->_var = $var;
parent::__construct($config);
}
now you should be able to set your variable and pass any configuration values you need to pass.
However it would be best to avoid the constructor and use the supplied init() method if at all possible.
<?php
class myClass extends Zend_Db_Table_Abstract
{
protected $_name = 'table_name';
private $_var;
public function init()
{
$this->_var = $var;
}
public function getById($id)
{
$select = $this->select()->where('id =?',$id);
return $this->fetchRow($select);
}
}
Related
I am building an MVC component and I'm getting stuck with an issue with a parent and child model. I have a few methods in the parent Model and they're not working with the database_class object
the constructor works fine
but when I use that object in the methods its like the constructor doesn't exist?
Class Controlller
{
public function __construct()
{
$this->childModel = $this->model('childModel');
} // end construct
// methods go here
}
Here are the models:
class childModel extends parentModel {
private $dbo;
public function __construct()
{
$dbobj = new Database_class;
$this->dbo = $dbobj;
}
//methods
}
class parentModel {
private $dbom;
public function __construct()
{
$dbombj = new Database_class;
$this->dbom = $dbombj;
var_dump($this->dbom); //working perfectly as database object
}
public function methodName()
{
var_dump($this->dbom); //not showing up as database object
}
}
I don't think this code is doing what you think it's doing. In childModel, you are overwriting the __construct method of the parentModel, so the __construct in the parentModel never gets called. Therefore $this->dbom should be null. Furthermore if you wish to use $this->dbom from the childModel, you should probably change the scope from private $dbom to protected $dbom. See this page for more info on that: http://php.net/manual/en/language.oop5.visibility.php
I have class DbTable, which implements all db queries to database such as insertRecord, updateRecord, ... But variable is not rewriting.
abstract class DbTable {
public static $table;
public static function insertRecord($data) {
// here I add some values to data, but that's not important
my_db::insert(self::$table, $data);
}
}
class User extends DbTable {
public static $table = 'table_users';
}
// everywhere I can call
User::insertRecord($data);
I know I can call
$c = get_called_class();
my_db::insert($c::$table, $data);
but I think that's not best solution at all.
Method and variables can be non static, I just use them because it is comfortable to write User::insertRecord instead of $user = new User(); $user->insertRecord($data);
When you're working with static classes you need to specify your variable source, in this case you're scoping to both classes and not on single class, this makes a difference, because self is scoping to concurrent class and when you want to scope for both classes you have to use static.
/**
* For testing
*/
class my_db {
public static function insert($table, $data){
echo $table;
}
}
abstract class DbTable {
public static $table = null;
public static function insertRecord($data) {
//self::$table is empty
//static::$table has 'table_users'
// here I add some values to data, but that's not important
my_db::insert(static::$table, $data);
}
}
class User extends DbTable {
public static $table = 'table_users';
}
// everywhere I can call
User::insertRecord(['Hi']);
self::$table is empty
static::$table has 'table_users'
You can read more about this here: SO Answer and PHP Documentation
Use static variables are unnecessary in this case. You just need dynamically create User object and them call method.
abstract class DbTable
{
protected $tableName;
public static function insertRecord($data)
{
$object = static::newInstance();
$object->insert($data);
}
public static function newInstance()
{
$className = get_called_class();
return new $className();
}
public function insert($data)
{
my_db::insert($this->tableName, $data);
}
}
class User extends DbTable
{
public function __construct()
{
$this->tableName = 'table_users';
}
}
You can now call:
User::insertRecord(['col1' => 'val1']);
But also you can insert rows from instated object:
$user = new User();
$user->insert(['col1' => 'val1']);
A parent class is constructed from outside the child class, thus, it's constructor cannot be called from inside the child. How should one go about accessing properties of the parent from the child in this case.
Example:
class MyParent {
protected $args;
protected $child;
public function MyParent($args=false){
$this->args=$args;
$this->child=new MyChild();
}
public function main(){
$this->child->printArgs();
}
}
class MyChild extends MyParent{
public function MyChild(){}
public function printArgs(){
Echo "args: ".$this->args['key']." = ".$this->args['value']."\n";
}
}
$parent=new MyParent(array('key'=>'value'));
$parent->main();
Empty variables are returned when run:
jgalley#jgalley-debian:~/code/otest$ php run.php
args: =
__construct() is the constructor. You are using a variant from ancient PHP4-times.
You instanciate two completely different objects, therefore of course the property $args is completely independent.
abstract class MyParent {
protected $args;
public function __construct($args=false){
$this->args=$args;
}
public function main(){
$this->printArgs();
}
abstract public function printArgs();
}
class MyChild extends MyParent{
public function printArgs(){
Echo "args: ".$this->args['key']." = ".$this->args['value']."\n";
}
}
$$object=new MyChild(array('key'=>'value'));
$object->main();
This at least works, but a problem is, that I don't know exactly what are the design goals. Because it seems to be a kind of cli-Application you should have a look at existing solutions to get an idea, how it could get solved.
I am having problems creating a variable with data from another class. Here is what I am doing...
<?PHP
class Customers extends Controller {
private $foo = $this->session->userdata('foo');
}
You probably want something more like this:
class Customers extends Controller
{
private $foo;
public function __construct()
{
parent::__construct();
$this->foo = $this->session->userdata('foo');
}
}
It's hard to know for sure without knowing more about your project.
You can set it with constructor because you are inhering from parent class:
class Customers extends Controller {
private $foo = null;
function __construct(){
parent::__construct();
$this->foo = $this->session->userdata('foo');
}
}
This is not possible: $this doesn't exist at the moment when you define the class, and you can't call functions at all at this point at all.
You will need to assign $foo in the constructor, after $this->session has been initialized. (#konforce beat me to the example.)
I want to do something like the following:
class SomeOtherClass {}
class Test
{
public $member = new SomeOtherClass();
}
The only problem is that I do not want to use a constructor, because the 'Test' class should extend another class and should not override the constructor.
Is this actually possible in PHP?
You can extend parent constructor in Test like this:
class Test extends SomeClass
{
public $member;
function __construct() {
$this->member = new SomeOtherClass();
parent::__construct();
}
}