I have a if else statement in codeigniter and try to direct different view but its gives error to me.However when i echo it prints to screen
This works !
public function kayitEmailOnay() {
$registrationCode = $this->uri->segment(3);
if ($registrationCode == '') {
echo "URLde onay kodu yok";
}
$registrationConfirmed = $this->kayitmodel->uyeOnay($registrationCode);
if ($registrationConfirmed)
echo "true";
else
echo "false";
}
This does not work
public function kayitSon() {
$this->load->view("kayit/kayitTamamla");
}
public function kayitHata() {
$this->load->view("kayit/kayitHata");
}
public function kayitEmailOnay() {
$registrationCode = $this->uri->segment(3);
if ($registrationCode == '') {
echo "URLde onay kodu yok";
}
$registrationConfirmed = $this->kayitmodel->uyeOnay($registrationCode);
if ($registrationConfirmed)
kayitSon();
else
kayitHata();
}
The error is that :
you should call your function like this:
$this->kayitSon();
$this->kayitHata();
Class method is called with the object or the object reference. if you are calling a class method inside that class use $this keyword
Try calling with $this
$this->kayitSon();
Related
I am trying to get a dynamic variable value in a PHP class but not sure how to do this. Here's my code:
<?php
class Test
{
public $type = "added";
public $date_added;
public function set_status()
{
$this->date_added = "Pass";
}
public function get_status()
{
echo $this->date_{$type};
}
}
$test = new Test();
$test->set_status();
$test->get_status();
?>
I am getting following error:
Notice: Undefined property: Test::$date_ in...
Notice: Undefined variable: type in ...
If I write echo $this->date_added; in place of echo $this->date_{$type}; then I get output "Pass".
How to fix it and do it properly?
Since you're using variable variables, put them in quotes, then concatenate:
echo $this->{'date_' . $this->type};
// not $type, use `$this->` since it's part of your properties
Or using via formatted string (double quotes will work as well):
echo $this->{"date_{$this->type}"};
<?php
class Test
{
public $type = "added";
public $date_added;
public function set_status()
{
$this->date_added = "Pass";
}
public function get_status()
{
echo $this->{'date_' . $this->type};
}
}
$test = new Test();
$test->set_status();
$test->get_status();
?>
You can do it multiple ways, date_{$type} is not valid expression and to acccess the class property you have to use this keyword .
class Test
{
public $type = "added";
public $date_added;
public function set_status()
{
$this->date_added = "Pass";
}
public function get_status()
{
$prop = 'date_'.$this->type;
echo $this->{'date_'.$this->type}; # one way to do it
echo $this->$prop; # another way to do it
echo $this->{"date_{$this->type}"}; # another way to do it
}
}
$test = new Test();
$test->set_status();
$test->get_status();
I am having trouble getting value of the class member setting in another method within the class. I have tried use __get and __set magic methods, getter and setter as well as two more approach as in the code but none of them working.
What I am looking is if the type is U than the javascript variable should be used else not.
Approach one
class UserRatings extends User {
private $postType; // string
public function headJS(){
// access postType value from getItem method
// this outputs nothing (blank)
if ($this->postType = 'U') {
# code...
}
}
public function getItem($post){
$this->postType = $post['data']['post_type'];
$markup = 'html markup to render the output';
return $this->postType;
}
public function isType($post)
{
if ($post == 'U') {
$this->isType = true;
}
return $this->isType;
}
}
Approach two
class UserRatings extends User {
private $isType = false;
public function headJS(){
// even this doesnt't work too
if ($this->isType) {
# code...
}
}
public function getItem($post){
$markup = 'html markup to render the output';
$type = $post['data']['post_type'];
$this->isType($type);
}
public function isType($post)
{
if ($post == 'U') {
$this->isType = true;
}
return $this->isType;
}
}
You first approach will not work as $isType will always be false. Because it’s not initialized and even when you initialize it with your function isType($post) you give it trueas a value. However you check in your headJS() if $this->isType ==‘U’ so always false.
For the second approach everything seems fine. My only guess is that you are calling HeadJS() before isType($post) or the value of $post is always different than ‘U’
you have missed $ sign at $this->isType(type);.
You have to just call $this->headJS(); after $this->isType = true;
class UserRatings extends User {
private $isType = false;
public function headJS(){
// even this doesnt't work too
if ($this->isType) {
# code...
}
}
public function getItem($post){
$markup = 'html markup to render the output';
$type = $post['data']['post_type'];
$this->isType($type);
}
public function isType($post)
{
if ($post == 'U') {
$this->isType = true;
$this->headJS();
}
return $this->isType;
}
}
Say I have to similar function :
public function auth(){
return $someResponse;
}
public function collect(){
return $someOtherResponse
}
Question : When one of the response get passed to another class, is there any way to check which function returned the response ?
In a purely object-oriented way, wanting to attach information to a value is akin to wrapping it into a container possessing context information, such as:
class ValueWithContext {
private $value;
private $context;
public function __construct($value, $context) {
$this->value = $value;
$this->context = $context;
}
public value() {
return $this->value;
}
public context() {
return $this->context;
}
}
You can use it like this:
function auth()
{
return new ValueWithContext($someresponse, "auth");
}
function collect()
{
return new ValueWithContext($someotherrpesonse, "collect");
}
This forces you to be explicit about the context attached to the value, which has the benefit of protecting you from accidental renamings of the functions themselves.
As per my comment, using arrays in the return will give you a viable solution to this.
It will allow a way to see what has been done;
function auth()
{
return (array("auth" => $someresponse));
}
function collect()
{
return (array("collect" => $someotherrpesonse));
}
class myClass
{
function doSomething($type)
{
if (function_exists($type))
{
$result = $type();
if (isset($result['auth']))
{
// Auth Used
$auth_result = $result['auth'];
}
else if (isset($result['collect']))
{
// Collect used
$collect_result = $result['collect'];
}
}
}
}
It can also give you a way to fail by having a return array("fail" => "fail reason")
As comments say also, you can just check based on function name;
class myClass
{
function doSomething($type)
{
switch ($type)
{
case "auth" :
{
$result = auth();
break;
}
case "collect" :
{
$result = collect();
break;
}
default :
{
// Some error occurred?
}
}
}
}
Either way works and is perfectly valid!
Letting the two user defined functions auth() & collect() call a common function which makes a call to debug_backtrace() function should do the trick.
function setBackTrace(){
$backTraceData = debug_backtrace();
$traceObject = array_reduce($backTraceData, function ($str, $val2) {
if (trim($str) === "") {
return $val2['function'];
}
return $str . " -> " . $val2['function'];
});
return $traceObject;
}
function getfunctionDo1(){
return setBackTrace();
}
function getfunctionDo2(){
return setBackTrace();
}
class DoSomething {
static function callfunctionTodo($type){
return (($type === 1) ? getfunctionDo1() : getfunctionDo2());
}
}
echo DoSomething::callfunctionTodo(1);
echo "<br/>";
echo DoSomething::callfunctionTodo(2);
/*Output
setBackTrace -> getfunctionDo1 -> callfunctionTodo
setBackTrace -> getfunctionDo2 -> callfunctionTodo
*/
The above function would output the which function returned the response
How to check call_user_func_array return data PHP:
i have class 1:
class MyClass
{
public function show()
{
echo 'This is show function';
}
}
class 2:
class MyClass2
{
public function show2()
{
return 'This is show2 function';
}
}
now, i check:
if(isset(call_user_func_array(array(new MyClass,'show'),$params)))
{
echo 'Has return data';
}
else
{
call_user_func_array(array(new MyClass,'show'),$params);//run normal function
}
if(isset(call_user_func_array(array(new MyClass2,'show2'),$params)))
{
echo 'Has return data';
}
else
{
call_user_func_array(array(new MyClass2,'show2'),$params);//run normal function
}
but not working, somebody can help me?
Warning
isset() only works with variables as passing anything else will result in a parse error. For checking if constants are set use the defined() function.
http://www.php.net/manual/en/function.isset.php
How can i pass a class as a parameter in my function
So far i've tried
$sc = new SampleClass();
SampleFunction($sc);
function SampleFunction(&$refClass)
{
echo $refClass->getValue();
}
this is a simplified example of what im doing.. i actually have to do complex procedures inside this sample function. I'm not getting any response from the sample function. What am i doing wrong? thank you
UPDATE
char.php
class Charss {
var $name=0;
var $hp=500;
var $spd=10;
var $rtime=10;
var $dmg=10;
function __construct( $name, $hp, $spd, $rtime , $dmg) {
$this->name = $name;
$this->hp = $hp;
$this->spd = $spd;
$this->rtime = $rtime;
$this->dmg = $dmg;
}
function get_name() {
return $this->name;
}
function set_name($new_name) {
$this->name = $new_name;
}
function get_hp() {
return $this->hp;
}
function set_hp($new_hp) {
$this->hp = $new_hp;
}
function get_spd() {
return $this->spd;
}
function set_spd($new_spd) {
$this->spd = $new_spd;
}
function get_rtime() {
return $this->rtime;
}
function set_rtime($new_rtime) {
$this->rtime = $new_rtime;
}
function get_dmg() {
return $this->get_dmg;
}
function set_dmg($new_dmg) {
$this->dmg = $new_dmg;
}
}
myclass.php
require("char.php");
class Person {
function try_process()
{
$chr1 = new Charss("Player1",500,3,0,50);
$chr2 = new Charss("Player2",500,6,0,70);
while ($chr1->get_hp() > 0 && $chr2->get_hp() > 0)
{
$sth = min($chr1->get_rtime(), $chr2->get_rtime());
if ($chr1->get_rtime() == 0 && $chr2->get_rtime() > 0)
{
exit;
Fight($chr1,$chr2);
$chr1->set_rtime($chr1->get_spd());
}
elseif ($chr2->get_rtime() == 0 && $chr1->get_rtime() > 0)
{
Fight($chr2,$chr1);
$chr2->set_rtime($chr2->get_spd());
}
else
{
Fight($chr1,$chr2); #having trouble with this
$chr1->set_rtime($chr1->get_spd());
}
$chr1->set_rtime($chr1->get_rtime() - $sth);
$chr2->set_rtime($chr2->get_rtime() - $sth);
}
}
function Fight($atk,$def)
{
$def->set_hp($def->get_hp() - $atk->get_dmg());
echo $atk->get_name() . " attacked " . $def->get_name() . " for " . $atk->get_dmg() . " damage";
}
}
so im calling the function try_process on button click
What you're actually doing there is passing an object, not a class.
$sc = new SampleClass();
creates an instance of SampleClass, aka an object.
I assume there's some error being thrown elsewhere as what you have is correct.
I tested the following code and got the expected output:
class SampleClass
{
public function getValue()
{
return 4;
}
}
$sc = new SampleClass();
SampleFunction($sc);
function SampleFunction(&$refClass)
{
echo $refClass->getValue();
}
Output: 4
If you provide more details of your actual code we might be able to determine the problem.
I can't see anything wrong with your code
using &$refClass is however is not recommended and I guess willbe removed from future iteration of PHP version
but here is an example
class objects are passed as reference I suppose so no need of '&'
http://ideone.com/GbmUy
Why is the function argument a reference? Probably shouldn't be.
Other than that, there's nothing wrong with you posted, so the error is likely within SampleClass.
Others have answered pretty well, but this is a silly little example to show you how to modify the class (either by calling a property setter, or setting public properties directly)
class foo {
private $member1;
public $member2;
public function __construct($member1,$member2) {
$this->member1=$member1;
$this->member2=$member2;
}
public function SetMember1($value) {
$this->member1 = $value;
}
public function GetMember1() {
return $this->member1;
}
}
function SetMembers(foo $obj, $member1, $member2) {
// Call a setter
$obj->SetMember1($member1);
// Set a member variable directly
$obj->member2 = $member2;
}
$obj = new foo('default member 1', 'default member 2');
echo "member1 (before): {$obj->GetMember1()}\n";
echo "member2 (before): {$obj->member2}\n";
// Change values
SetMembers($obj, 'new member1', 'new member2');
echo "member1 (after): {$obj->GetMember1()}\n";
echo "member2 (after): {$obj->member2}\n";
This will output:
member1 (before): default member 1
member2 (before): default member 2
member1 (after): new member1
member2 (after): new member2