Function($param) add to function __construct - php

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;
}

Related

Use a Callback on PHP chained methods

Let's say you have a class like:
class Foo
{
// maybe private or protected?
public Pages $pages;
public Owners $owners;
// ...
}
How can I implement methods that would accept closures in a chained fashion? Every method call in the chain should update the internal class properties (so they are using the latest).
Usage examples:
$pagesArray = Foo::wherePage('page', function ($page, $owners) {
// do something with the given page, owners is optional
})->b(function ($owners) {
// now do something with owners, e.g. $owners->foo();
})->getPagesArray();
Or
$pagesArray = Foo::pages(function ($pages, $owners) {
// loop pages, owners is optional
})->b(function ($owners) {
// now do something with owners, e.g. $owners->foo();
})->getPagesArray();
Thanks!
<?php
class Foo
{
protected $pages = ['abc'];
public static function setPages($pages)
{
call_user_func($pages, ['aaa']);
$thisClass = new static();
return $thisClass;
}
public function owners($owners)
{
call_user_func($owners, ['john', 'wick']);
return $this;
}
public function getPages()
{
return $this->pages;
}
}
$Foo = Foo::setPages(function($pages) {
var_dump($pages);
})->owners(function($owners) {
var_dump($owners);
})->getPages();
var_dump($Foo);

Get Class to know a Variable

I'm facing a problem, I want the class Page to know the Variable '$format'.
// class1.php
<?php
include('./class2.php');
echo $format->getTest(); // returns :-) (declared in class2.php)
class Page {
PUBLIC function getText() {
return $format->getTest(); // returns Call to a member function getTest() on null
}
}
$page = new Page;
?>
// class2.php
<?php
class Format {
PUBLIC function getTest() {
return ":-)";
}
}
$format = new Format;
?>
Any suggestions/ideas?
EDIT:
I found a way: return $GLOBALS['format']->getTest();
But I dont like it, its so much to type. Any other way(s)?
Philip
Proper objective solution is to pass variable to constructor, setter or as argument to getText() method. Choose one you find most appropriate for your case.
Constructor
class Page
{
private $format;
public function __construct(Format $format)
{
$this->format = $format;
}
public function getText()
{
return $this->format->getTest();
}
}
$page = new Page($format);
echo $page->getText();
Setter
class Page
{
private $format;
public function setFormat(Format $format)
{
$this->format = $format;
}
public function getText()
{
return $this->format->getTest();
}
}
$page = new Page;
$page->setFormat($format);
echo $page->getText();
Argument
class Page
{
public function getText(Format $format)
{
return $format->getTest();
}
}
$page = new Page;
echo $page->getText($format);

passing variables from a protected function to a public function inside the same class in php

I have a class and two functions inside it as follows:
class MyClassName
{
protected function myFunction1()
{
// some code here
return $something;
}
public function myFunction2()
{
// some code here
return $somethingElse;
}
}
What I need to do is define a variable in myFunction1() and then use it in myFunction2(). What is the best practice to do that?
class MyClassName
{
public $var = 0;
protected function myFunction1()
{
// some code here
$this->var = ...;
return $something;
}
public function myFunction2()
{
// some code here
echo $this->var;
return $somethingElse;
}
}
Actually vars should be defined out of the function and then set a value. Then can be modified over all the script, by doing this->var
Make it a class property
class MyClassName
{
private $property;
public function __construct() {
$this->myFunction1();
}
protected function myFunction1()
{
// some code here
$this->property = 'an apple';
}
public function myFunction2()
{
// some code here
return $this->property;
}
}
Now test it:
$my_class = new MyClassName();
$something = $my_class->myFunction2();
echo $something;

How to turn a zend helper to a singleton?

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.

PHP How to distinguish $this pointer in the inheritance chain?

Please look at the following code snipped
class A
{
function __get($name)
{
if ($name == 'service') {
return new Proxy($this);
}
}
function render()
{
echo 'Rendering A class : ' . $this->service->get('title');
}
protected function resourceFile()
{
return 'A.res';
}
}
class B extends A
{
protected function resourceFile()
{
return 'B.res';
}
function render()
{
parent::render();
echo 'Rendering B class : ' . $this->service->get('title');
}
}
class Proxy
{
private $mSite = null;
public function __construct($site)
{
$this->mSite = $site;
}
public function get($key)
{
// problem here
}
}
// in the main script
$obj = new B();
$obj->render();
Question is: in method 'get' of class 'Proxy', how I extract the corresponding resource file name (resourceFile returns the name) by using only $mSite (object pointer)?
What about:
public function get($key)
{
$file = $this->mSite->resourceFile();
}
But this requires A::resourceFile() to be public otherwise you cannot access the method from outside the object scope - that's what access modifiers have been designed for.
EDIT:
OK - now I think I do understand, what you want to achieve. The following example should demonstrate the desired behavior:
class A
{
private function _method()
{
return 'A';
}
public function render()
{
echo $this->_method();
}
}
class B extends A
{
private function _method()
{
return 'B';
}
public function render()
{
parent::render();
echo $this->_method();
}
}
$b = new B();
$b->render(); // outputs AB
But if you ask me - I think you should think about your design as the solution seems somewhat hacky and hard to understand for someone looking at the code.

Categories