PHP Call a function from another function in same class - php

I am unable figure out how to make this work any help will be appreciated
<?php
class some{
function display()
{
$w ="its working";
$this->show($w);
}
function show($s)
{
echo $s;
}
}
?>

You were rightly advised to create an instance of your class then call the method on it but you said
see thats what i don't want .....i want some way to make it work without adding those two lines...by doing something else...just not that...and i can't figure out what i can do.
That something else is Simple! Make your method static
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class.
public static function display()
{
$w ="its working";
self::show($w);
}
Then you can just do
some::display();
Fiddle

well it is working if you add the last two lines:
<?php
class some{
function display()
{
$w ="its working";
$this->show($w);
}
function show($s)
{
echo $s;
}
}
$x = new some;
$x->display();
?>
see here and click on "execute code"

Seems you are not called to display() function. Call to that function and try again.

Related

PHP function working differently for return and echo. Why?

By using the following class:
class SafeGuardInput{
public $form;
public function __construct($form)
{
$this->form=$form;
$trimmed=trim($form);
$specialchar=htmlspecialchars($trimmed);
$finaloutput=stripslashes($specialchar);
echo $finaloutput;
}
public function __destruct()
{
unset($finaloutput);
}
}
and Calling the function, by the following code, it works fine.
<?php
require('source/class.php');
$target="<script></script><br/>";
$forminput=new SafeGuardInput($target);
?>
But if in the SafeGuardInput class if I replace echo $finaloutput; with return $finaloutput; and then echo $forminput; on the index.php page. It DOES NOT WORK. Please provide a solution.
You can't return anything from a constructor. The new keyword always causes the newly created object to be assigned to the variable on the left side of the statement. So the variable you've used is already taken. Once you remember that, you quickly realise there is nowhere to put anything else that would be returned from the constructor!
A valid approach would be to write a function which will output the data when requested:
class SafeGuardInput{
public $form;
public function __construct($form)
{
$this->form=$form;
}
public function getFinalOutput()
{
$trimmed = trim($this->form);
$specialchar = htmlspecialchars($trimmed);
$finaloutput = stripslashes($specialchar);
return $finaloutput;
}
}
Then you can call it like in the normal way like this:
$obj = new SafeGuardInput($target);
echo $obj->getFinalOutput();

mail function not working in OO PHP

I tried the following:
a.php
<?php
class Page
{
function go(){
echo "1";
send();
}
function send(){
mail("whatever#gmail.com","subj","hi");
}
}
?>
b.php
<?php
require("a.php");
$page=new Page();
$page->go();
?>
b.php doesn't neither sends a mail nor t echo anything. When I put echo before send in the function go(), PHP echo "1" but doesn't send anything. I thought maybe there's something wrong with the mail() function so I changed b.php to:
<?php
require("a.php");
$page=new Page();
$page->send();
?>
and everything works fine. What is the problem with the initial code?
This isn't a mail function. You are trying to call a function that isn't labeled correctly. In this type you're attempting to call a static function. But the function you're calling is not labeled static. You can fix this a few different ways however.
Your echo doesn't work because it can't find the function you're referencing in go().
// best use
class Page{
public function go(){
echo "yay";
$this->send();
}
...
}
or
// static isn't recommended most often, but this will work.
class Page{
// same go function
public static function send(){
// same
}
}
Been awhile since I've used PHP, but you have to use $this to call the send method from a class method. See below:
<?php
class Page
{
function go(){
echo "1";
$this->send(); # Added the $this-> to the function call
}
function send(){
mail("whatever#gmail.com","subj","hi");
}
}
?>
Change send();
to
$this->send();
send() is a class method not a function, hence it cannot be called without $this in this case.

Having problems on fetching variables on a class in PHP

I just want to ask if its possible to call variables on class to another page of the site. I have tried calling the function's name and inside the parenthesis. I included the variable found inside that function e.g:
<?php
$loadconv -> loadmsg($msgReturn);
echo $loadconv;
?>
But it didn't work.
Do you want something like this?
class Load
{
public $msgReturn;
__construct()
{
}
public function loadMsg($param)
{
$this->msgReturn = $param;
}
}
Then you could do
$loadConv = new Load();
$loadConv->loadMsg('just a string');
echo $loadConv->msgReturn; // 'just a string'

Setting Up PHP Class's without a framework

Does anyone have a guide or can provide me with a quick example how to set up a class and pass the data from the class to the index.php?
I was thinking I could do it like I do in a framework can I not?
$this->class->function();
coolclass.php
class coolClass
{
public function getPrintables()
{
return "hello world!";
}
}
index.php
include 'coolclass.php';
$instance = new coolClass();
echo $instance->getPrintables();
A quick example:
Foo.php
class Foo {
public __construct() {
..initialize your variables here...
}
public function doSomething() {
..have your function do something...
}
}
Index.php:
Include('Foo.php');
$my_Foo = Foo();
$my_Foo->doSomething();
I think its fairly straight-forward as to what's happening here...so ill leave it at that. Don't want to give too much away.

Why won't the shuffle function work in a PHP class?

Why won't it shuffle the array so I get a random result each time?
class greeting {
public $greet = array('hi','hello');
shuffle($greet);
}
$hi = new greeting;
echo $hi->greet[1];
Is their something wrong with my code?
If you change it so the shuffle is inside the constructor it should work fine.
class greeting {
public $greet = array('hi','hello');
function __construct(){
shuffle($this->greet);
}
}
any calculation can not be executed outside the method, inside class.
class greeting {
public $greet = array('hi','hello');
function __construct()
{
shuffle($this->greet);
}
}
$hi = new greeting;
echo $hi->greet[1];
Inside a class block you can only define constants, properties (both with fixed values) and methods. You can't put code in that block, code can only be placed inside methods (AKA functions).

Categories