How to turn a zend helper to a singleton? - php

I need to use a view helper to make counts in a bunch of different partials.
In the partials I can't access view variables, but I can access helpers, so I created this simple class.
class Zend_View_Helper_Counter extends Zend_View_Helper_Abstract{
protected $count = 0;
public function counter(){
return $this;
}
public function add($i = 1){
$this->count = $this->count + (int) $i;
return $this;
}
public function get(){
return $this->count;
}
public function set($count){
$this->count = (int) $count;
return $this;
}
}
However this <?php echo $this->counter()->add()->get()?> Always returns 1. I guess this is because it's always a different instance of the class. How would I need to change the counter() function so that it can count through all the views and partials?

Use statics:
static protected $count = 0;
public function add($i = 1){
self::$count = self::$count + (int) $i;
return $this;
}
Write a separate counter singleton and then do:
public function get(){
return Counter::getInstance();
}
public function add($i = 1){
Counter::getInstance()->add($i);
return $this;
}
If you want, you may also extend it by using named counters and then $count would be an array.

Related

class __constructor don't return zerofill number

I have this class :
class codici {
public $i;
public $len;
public $str;
public $type;
function __construct()
{
$this->getPad($this->i);
}
public function getPad($i)
{
return ''.str_pad($i,4,'0',0);
}
}
And I use it in this way :
$cod = new codici();
$cod_cliente = $cod->i = 1; //return 1
$cod_cliente = $cod->getPad(1); //return 0001
If I call the class direct, __constructor call internal method getPad and returns wrong answer '1'. Instead, if I call the method getPad return the correct value '0001'.
Why can't I use $cod_cliente=$cod->i=1 ?
$cod_cliente = $cod->i = 1;
It will set value for $cod_cliente and $cod->i both to 1. So when you print $cod_cliente, it will show 1.
But in case $cod_cliente = $cod->getPad(1), code to add padding executes and return 0001.
If you want your constructor to return something you should give it a parameter. And since your getPad($i) returns something you'd need to echo/print the results.
<?php
class codici {
public $i;
public $len;
public $str;
public $type;
function __construct($parameter)
{
$this->i = $parameter;
echo $this->getPad($this->i);
}
public function getPad($i)
{
return ''.str_pad($i,4,'0',0);
}
}
This will allow you to call your class like this:
$c = new codici(3);
which would echo 0003.
this is right code:
class codici {
public $i;
public $len;
public $str;
public $type;
function __construct($parameter)
{
$this->i = $this->getPad($parameter);
}
public function getPad($i)
{
return str_pad($i,4,'0',0);
}
}
now work:
$c= new codici(1);
echo $c->i;//return 0001
echo $c->getPad(1);//return 0001
thank a lot.

PHP Codeigniter :How to use normal variable from one function to another function

I have a function like below, I want to use $totaldays variable into calculate function
public function add()
{
$totaldays = 10;
}
public function calculate()
{
$result = 1000/$totaldays
}
You can use getter and setter methods to retrieve and set the variables
private $totalDays;
public function setTotalDays($days)
{
$this->totalDays= $days;
}
public function getTotalDays()
{
return $this->totalDays;
}
One of the best way to do is to create properties.
A class may contain its variables (called "properties"), so that you cant set and access it whenever you want.
class maths{
private $totaldays = 1;
public function add()
{
$this->totaldays = 10;
}
public function calculate()
{
$totaldays = $this->totaldays;
$result = 1000/$totaldays;
}
}
You pass the values to the functions and return back the return value.
function add($x){
$totaldays = $x;
Return $totaldays;
}
function calculate($totaldays){
$result = 1000/$totaldays;
Return $result;
}
$totaldays = add(10);
Echo calculate($totaldays); //100
https://3v4l.org/7Usk2

Function($param) add to function __construct

I have two functions in a class:
public function __construct()
{
$this->page=$this->Pagination_page();
}
function Pagination_page($page){
return $page;
}
This version is not working. How can I add to the $this->page the Pagination_Page value?
To use a class in the manner you want it to, you will need to use getters and setters. Take a look at the following example:
class Pagination
{
private $page;
public function __construct($page = false)
{
if (false !== $page) {
$this->page = $page;
}
}
public function setPage($page)
{
$this->page = $page;
return $this;
}
public function getPage()
{
return $this->page;
}
}
$pagination = new Pagination('1');
$pagination->setPage('3');
print_r($pagination->getPage()); // returns 3
It's not working because the constructor is initialized when you instanciate the object - it's 0 there. But you cannot assign this value of $page at the start you must call the method first. If you want to do this you should create it in the construct directly:
public function __construct($_page)
{
$this->page=$_page;
}
If you want to use the "thing" you created then:
public function __construct($_page)
{
$this->page=$this->Pagination_page($_page);
}
function Pagination_page($page){
return $page;
}

How to pass a chained method as an argument for another method in the same class?

I have a class with a bunch of chained methods. Here is an example:
class Sum {
public static $res = [];
private static $instance = null;
public static function run() {
if (self::$instance === null)
self::$instance = new self;
return self::$instance;
}
public function res() {
return self::$res;
}
public function addTen($int) {
self::$res = $this->addFour($str) + 6;
return $this;
}
public function addFour($int) {
self::$res = $int + 4;
return $this;
}
}
So if I want to call the addTen() method I can do like so:
echo Sum::run()->addFour(5)->res(); // Works, returns 9
echo Sum::run()->addTen(5)->res(); // Doesn't work
The above code doesn't work because the chained methods return the current object from the Sum class. So I managed to fix this by changing the addTen() method so it calls the res() method after the addFour() method like so:
public function addTen($int) {
self::$res = $this->addFour($str)->res() + 6;
return $this;
}
In the above case, that is ok because there is only on method being called from inside the addTen() method but what if I need to call a lot of other chained methods from inside the addTen() method? How can I do so the res() method is no longer needed to be called after every single call from another chained method inside the class (it could become unhandable to have a lot of "->res()" being called everywhere in the class).
I do not know what is your task for this class, but manually writing "add" per function will not make your class adaptable. As I have noted, you have used an array and not chain the $res properly. Since this is a sum class I would expect that you want to sum up the chain.
so I rewrote your class:
<?php
class Sum {
public static $res = [];
private static $instance = null;
public static function run() {
if (self::$instance === null)
self::$instance = new self;
return self::$instance;
}
public function res() {
return array_sum(self::$res);
}
public function add($int) {
self::$res[] = $int;
return $this;
}
}
$sum = new Sum();
$x = $sum->add(5)->add(6)->res();
echo $x; // 11
and you can see it work here:
https://3v4l.org/itDHN

Yii: How to refresh or unset or reset a model?

I need to refresh, reset or unset a model;
Normally, by using a for operation, the public static $k value should change, and id does change, but the tableName model method is called only once;
the value of tablename will always be 1, because that is the fisrt value of $i;
for($i=1;$i<=100;$i++){
VillageBuildingKXSlaveM::$server_id = 1;
VillageBuildingKXSlaveM::$k = $i;
VillageBuildingKXSlaveM::model()->findAllByAttributes(array());
}
<?php
class VillageBuildingKXSlaveM extends VillageBuildingKXM {
public static function model($className = __CLASS__) {
return parent::model($className);
}
public static $server_id;
public static $slave_db;
public static $k;
public function getDbConnection() {
self::$slave_db = Yii::app()->dbx;
if (self::$slave_db instanceof CDbConnection) {
self::$slave_db->active = false;
$config = require(Yii::app()->getBasePath() . '/config/main.php');
$connectionString = $config['components']['dbx']['connectionString'];
self::$slave_db->connectionString = sprintf($connectionString, self::$server_id);
self::$slave_db->setActive(true);
return self::$slave_db;
} else
throw new CDbException(Yii::t('yii', 'Active Record requires a "db" CDbConnection application component.'));
}
public function tableName() {
return 'village_building_k' . self::$k;
}
}
Try using
VillageBuildingKXSlaveM::model()->unsetAttributes();
to unset the attributes in a model
Or you can also pass the attributes name as arguments in the method like
VillageBuildingKXSlaveM::model()->unsetAttributes($attributes);
You can call
VillageBuildingKXSlaveM::model()->tableName();

Categories