Error when using self static function - php

I have a sample code:
class Assets {
public static $my_static = '';
public static function custom_js()
{
return self::add_custom_js();
}
public static function add_custom_js($str)
{
return self::$my_static = $str;
}
}
And php
<?php
Assets::add_custom_js("ABC");
?>
<html>
...
<?php echo Assets::custom_js(); ?>
...
</html>
Error can't show data string is "ABC"

I think you want to return $my_static value to get ABC in echo Assets::custom_js();. Consider following:
class Assets {
public static $my_static = '';
public static function custom_js()
{
// return self::add_custom_js();
return self::$my_static; //This is what you want i believe
}
public static function add_custom_js($str)
{
return self::$my_static = $str;
}
}

You have to return $my_static from custom_js() so Your class should be as follow:
<?php
class Assets {
public static $my_static = '';
public static function custom_js()
{
return self::$my_static; //<-------------change this line
}
public static function add_custom_js($str="")
{
return self::$my_static = $str;
}
}
?>

Related

Removing duplicate-like function which uses static to denote the class name in PHP

Is there a way to get rid of the getCopy() method in the derived class HelloAndGoodbye, given that it looks the same as the getCopy() in the base class Hello?
And what is more, what is there an efficient way to achieve this?
(the only difference between the two functions is that in the baseclass 'static' refers to 'Hello' and in the derived class 'static' refers to 'HelloAndGoodbye; as for the variables contained therein they can be easily renamed so that they are the same in both functions).
<?php
class Hello {
private $hello;
public function createStub() {
return new static(null);
}
public function __construct($hello) {
$this->setHello($hello);
}
public function getCopy() {
$helloCopy = static::createStub();
$this->doCopy($helloCopy);
return $helloCopy;
}
public function doCopy($helloCopy) {
$helloCopy->setHello($this->hello);
}
public function setHello($hello) {
$this->hello = $hello;
}
public function getHello($hello) {
return $this->hello;
}
public function __toString() {
return $this->hello . "\n";
}
}
class HelloAndGoodbye extends Hello {
private $goodbye;
public function createStub() {
return new static(null, null);
}
public function __construct($hello, $goodbye) {
parent::__construct($hello);
$this->setGoodbye($goodbye);
}
public function getCopy() {
$helloAndGoodbyeCopy = static::createStub();
$this->doCopy($helloAndGoodbyeCopy);
return $helloAndGoodbyeCopy;
}
public function doCopy($helloAndGoodbyeCopy) {
parent::doCopy($helloAndGoodbyeCopy);
$helloAndGoodbyeCopy->setGoodbye($this->goodbye);
}
public function setGoodbye($goodbye) {
$this->goodbye = $goodbye;
}
public function getGoodbye($goodbye) {
return $this->goodbye;
}
public function __toString() {
return parent::__toString() . $this->goodbye . "\n";
}
}
function test() {
$hello = new Hello("Hello John");
$helloAndGoodbye = new HelloAndGoodbye("Hello Jane", "Goodbye Jane");
echo $hello;
echo $helloAndGoodbye;
}
test();
OUTPUT:
Hello John
Hello Jane
Goodbye Jane
I found a solution to the problem at hand by means of using the __CLASS__ PHP constant which correspond to the name of the class within which it appears. This allowed me to get rid of the pseudo-duplicate getCopy() method in the derived class, while still allowing getCopy() to work fine on both:
<?php
class Hello {
private $hello;
public function createStub() {
return new static(null);
}
public function __construct($hello) {
$this->setHello($hello);
}
public function getCopy() {
$class = __CLASS;
$instanceCopy = $class::createStub();
$this->doCopy($instanceCopy);
return $instanceCopy;
}
public function doCopy($helloCopy) {
$helloCopy->setHello($this->hello);
}
public function setHello($hello) {
$this->hello = $hello;
}
public function getHello($hello) {
return $this->hello;
}
public function __toString() {
return $this->hello . "\n";
}
}
class HelloAndGoodbye extends Hello {
private $goodbye;
public function createStub() {
return new static(null, null);
}
public function __construct($hello, $goodbye) {
parent::__construct($hello);
$this->setGoodbye($goodbye);
}
public function doCopy($helloAndGoodbyeCopy) {
parent::doCopy($helloAndGoodbyeCopy);
$helloAndGoodbyeCopy->setGoodbye($this->goodbye);
}
public function setGoodbye($goodbye) {
$this->goodbye = $goodbye;
}
public function getGoodbye($goodbye) {
return $this->goodbye;
}
public function __toString() {
return parent::__toString() . $this->goodbye . "\n";
}
}
function test() {
$hello = new Hello("Hello John");
$helloAndGoodbye = new HelloAndGoodbye("Hello Jane", "Goodbye Jane");
echo $hello;
echo $helloAndGoodbye;
}
test();
OUTPUT:
Hello John
Hello Jane
Goodbye Jane

Creating staitic method and calling the method in PHP

I'm getting value form Form class by creating object.It's working fine.But I wanna do it using static method.I tried but did not succeed.
public function display()
{
$newform=new Form();
echo "<pre>";
var_dump($newform->getAll());
var_dump($newform->get('name'));
}
<?php
class Form
{
private $value = array();
function __construct() {
// here you can use some validation or escapes
foreach($_POST as $key=>$value)
$this->value[$key] = $value;
}
public function getAll() {
return $this->value;
}
public function get($value) {
$this->value = $_POST[$value];
return $this->value;
}
}
Maybe You should just try to read PHP documentation about static keyword?
Example:
class Form {
private static $value = array();
public static function factory() {
// here you can use some validation or escapes
foreach($_POST as $key => $value) {
static::$value[$key] = $value;
}
}
public static function getAll() {
return static::$value;
}
public static function get($key) {
return static::$value[$key];
}
}
Use:
public function display() {
Form::factory();
echo "<pre>";
var_dump(Form::getAll());
var_dump(Form::get('name'));
echo "</pre>";
}
you do not declare a function as public/private/protected outside of the class
you you want to call this method statically, you may try it
<?php
function display()
{
$newform=new Form($_POST);
echo "<pre>";
var_dump(Form::getAll());
var_dump(Form::get('name'));
}
class Form
{
private static $value = array();
function __construct(){
// here you can use some validation or escapes
function __constract($array){
foreach($array as $key=>$value)
self::$value[$key] = $value;
}
}
public static function getAll(){
return self::$value;
}
public static function get($value){
self::$value = self::$value[$value];
return self::$value;
}
}
Here is an example for getAll method. For get method the same idea:
public function display()
{
var_dump(Form::getAll());
}
class Form
{
private static $value = array();
public static function initPost()
{
foreach($_POST as $key=>$value)
self::$value[$key] = $value;
}
public static function getAll()
{
return self::$value;
}
}

return data with chain static method

i'm trying to return data with chain static method , but i can't because the method return one thing only .
class Input
{
public static function set($input)
{
$data = $input;
$class = get_class();
return $data;
return self::$class = new $class;
}
public static function get()
{
echo ' - get method';
}
}
Input::set('ahmed')->get();
but it's only print " -get method "
I think you want
class Input
{
private static $data;
public static function set($input)
{
self::$data = $input;
return self;
}
public static function get()
{
echo self::$data.' - get method';
}
}
Input::set('ahmed')->get(); // ahmed - get method
but this you can use only once better is set name for value
class Input
{
private static $data = array();
public static function set($name, $input)
{
self::$data[$name] = $input;
return self;
}
public static function get($name)
{
echo self::$data[$name].' - get method';
}
}
Input::set('name', 'ahmed')->get('name'); // ahmed - get method

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;

PHP method chaining

Can anyone explain me why this code does not work (the content $this->_string) is empty?
<?php
class WordProcessor
{
public $_string = '';
public function __constructor($text)
{
$this->_string = $text;
}
public function toLowerCase()
{
$this->_string = strtolower($this->_string);
return $this;
}
public function trimString()
{
echo $this->_string;
$this->_string = trim($this->_string);
return $this;
}
public function capitalizeFirstLetter()
{
$this->_string = trim($this->_string);
return $this;
}
public function printResult()
{
echo $this->_string;
}
}
$data = new WordProcessor("here Are some words! ");
$data->trimString()->toLowerCase()->capitalizeFirstLetter()->printResult();
Use construct instead of constructor?
Its
public function __construct($text)
not __constructor(..)

Categories