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);
Related
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;
}
I'm wondering if its possible to switch the visibility in PHP. Let me demonstrate:
class One {
function __construct($id){
if(is_numeric($id)){
//Test function becomes public instead of private.
}
}
private function test(){
//This is a private function but if $id is numeric this is a public function
}
}
Is such thing even possible?
I would use an abstract class with two implementing classes: One for numeric and one for non-numeric:
abstract class One {
static function generate($id) {
return is_numeric($id) ? new OneNumeric($id) : new OneNonNumeric($id);
}
private function __construct($id) {
$this->id = $id;
}
}
class OneNumeric extends One {
private function test() {
}
}
class OneNonNumeric extends One {
public function test() {
}
}
$numeric = One::generate(5);
$non_numeric = One::generate('not a number');
$non_numeric->test(); //works
$numeric->test(); //fatal error
It can be faked up to a point with magic methods:
<?php
class One {
private $test_is_public = false;
function __construct($id){
if(is_numeric($id)){
$this->test_is_public = true;
}
}
private function test(){
echo "test() was called\n";
}
public function __call($name, $arguments){
if( $name=='test' && $this->test_is_public ){
return $this->test();
}else{
throw new LogicException("Method $name() does not exist or is not public\n");
}
}
}
echo "Test should be public:\n";
$numeric = new One('123e20');
$numeric->test();
echo "Test should be private:\n";
$non_numeric = new One('foo');
$non_numeric->test();
I haven't thought about the side effects. Probably, it's only useful as mere proof of concept.
My PHP codes :
class FrontEndController extends BaseController {
public function Welcome()
{
return View::make('frontend.index');
}
public function Modules($param="")
{
/* convert parameter "$param" be a function */
$prefix = "Mod_"; // func prefix's
if (function_exists($this->$prefix.$param)) {
return $this->$prefix.$param();
}else
{
return $param;
}
}
protected function Mod_product(){
return "You are called CATEGORY";
}
protected function Mod_promo(){
return "You are called PROMO";
}
}
When i use FrontEndController->Modules('product'); i want it return value from Mod_product(), so also when I use FrontEndController->Modules('promo'); it will return value from Mod_promo(). How to do this?
Use method_exists:
$prefix = "Mod_"; // func prefix's
$function = $prefix.$param;
if ( method_exists( $this, $function ) ) {
return $this->$function();
} else {
return $param;
}
You have a few mistakes in your code:
function_exists does not work for classes, use method_exists instead.
if (function_exists($this->$prefix.$param)) {
This does not work either, use call_user_func instead.
return $this->$prefix.$param();
Corrected code:
class FrontEndController extends BaseController {
public function Welcome()
{
return View::make('frontend.index');
}
public function Modules($param="")
{
/* convert parameter "$param" be a function */
$prefix = "Mod_"; // func prefix's
$fcn = $prefix.$param; // save name of function
if (method_exists($this, $fcn)) {
return call_user_func(array($this, $fcn));
}else
{
return $param;
}
}
protected function Mod_product(){
return "You are called CATEGORY";
}
protected function Mod_promo(){
return "You are called PROMO";
}
}
Update your Modules function like this
$this->{$prefix . $params}();
Hope it helps :)
The Reflection Class is the ideal tool for this. It's a Swiss army knife for classes.
public function Modules($param="")
{
/* convert parameter "$param" be a function */
$prefix = "Mod_"; // func prefix's
$reflection = new ReflectionClass(__CLASS__);
if($reflection->hasMethod($prefix.$param)) {
return $this->$prefix.$param();
}else
{
return $param;
}
}
How to display a class from another class in PHP ?
class Layout {
public $var;
public function __construct() {
$this->var = 'test';
}
public function __toString() {
return $this->var;
}
}
class Template {
private $var_layout;
public function __construct() {
$obj = new Layout;
$this->var_layout = $obj;
}
public function __toString() {
return $this->var_layout;
}
}
$template = new Template();
echo($template);
Error message: Method Template::__toString() must return a string value
Please help, thank you very much..
return $this->var_layout; in the Template class does not return a string, it returns an object. Make it return a string by calling the __toString() method of that object explicitly.
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.