1

I have follwing class in file "super.class.php"

<?php    
class super {

        public function loadme() {
            $tilt = "I am tilted";
            $straight = "I am Straight";
            $round = "I am round";
            $sleep = "I am Sleeping";
        }

        public function fireme() {
            $hit = "I am hit";
            $bullet = "I got bullet";
            $gun = "Cover me! I am receiving Heavy Firing";
        }
}
?>

I want to print each variable in my other file. Note: Classes are called from spl_autoload_register

When I am trying to print :-

<?php
    $obj_super = new super();
    echo $obj_super->loadme()->tilt;
    echo $obj_super->fireme()->hit;
?>

I am getting error :- Trying to get property of non-object in agent.php

Update: I got it working via this

Hope I am not doing any wrong below :-

<?php    
    class super {

        public $title = array();
        public $situation = array();

        public function loadme() {
            $this->title['tilt'] = "I am tilted";
            $this->title['straight'] = "I am Straight";
            $this->title['round'] = "I am round";
            $this->title['sleep'] = "I am Sleeping"; 
            return $this->title;
        }

        public function fireme() {
            $this->situation['hit'] = "I am hit";
            $this->situation['bullet'] = "I got bullet";
            $this->situation['gun'] = "Cover me! I am receiving Heavy Firing";
            return $this->situation;
        }
    }
    ?>

    <?php
        $obj_super = new super();
        $value = $obj_super->loadme();
        echo $value['tilt'];
    ?>

Thanks

9
  • All those are local variables, they're not attached to the instance. $this->tilt = ..., etc... Commented Sep 21, 2013 at 4:02
  • i even tried using public $variable and than global $variable inside function , it's still not working :( Commented Sep 21, 2013 at 4:03
  • If you really want to do it this way, you need to create a new object in each method (e.g. stdClass) and assign the local variables to it as properties, then return that object. It's very messy though - what exactly are you trying to do? Commented Sep 21, 2013 at 4:03
  • 1
    avoid global. it is pure evil. Commented Sep 21, 2013 at 4:06
  • 1
    Johny, no. @elclanrs I think making them public properties would be even worse. At the moment it's looking like loadme and fireme should each be a separate class. Commented Sep 21, 2013 at 4:13

6 Answers 6

2

Ok, you seem to have misunderstood how functions work in PHP - the variables you have declared in your functions are local to the function - i.e. they only have a value inside the functions scope.

Secondly, the notation $obj_super->loadme()->tilt will give the value of the property tilt of the return of loadme() since loadme() doesnt return anything - you are essentially doing this null->tilt. Which likely causes all sorts of errors if you check your log.

What is it you are trying to achieve here?

Sign up to request clarification or add additional context in comments.

7 Comments

What do you mean by property of return ? Where did you read that ? It is property of object inside which function defined as a method, i.e. $this->property.
Well, if I return an object in my method, then use the -> operator, I access the property of the object I returned.
Can i return multiple variable from function ?
@ZackNewsham sorry misunderstood that. Could you please add an example?
Only within an object, or an array, so you could do this $ret = new stdClass(); $ret->variable = "value"; return $ret;
|
2

You need to add the variables into your class, like so:

class super {
    public $tilt;
    public $straight;
}

And then set them with

public function loadme(){
    $this->tilt = "I am tilted";
    $this->straight = "I am straight";
}

You can get them with

$new = new super;
$new->loadme();
echo $new->tilt;

3 Comments

I need to call it only with the help of function :(
If you set them in the function, like public function(){ $this->tilt = 'whatever'; } then you can do $new->function(); and then echo $new->tilt;
I got it working via this phpfiddle.org/api/raw/xw7-smb . Hope I am not doing any wrong there.. If it is right can someone post this, so that i can accept as answer. I am not able to add my answer because of my less reputation
1

As requested - the answer from Johny Bravo.

class super {

    public $title = array();
    public $situation = array();

    public function loadme() {
        $this->title['tilt'] = "I am tilted";
        $this->title['straight'] = "I am Straight";
        $this->title['round'] = "I am round";
        $this->title['sleep'] = "I am Sleeping"; 
        return $this->title;
    }

    public function fireme() {
        $this->situation['hit'] = "I am hit";
        $this->situation['bullet'] = "I got bullet";
        $this->situation['gun'] = "Cover me! I am receiving Heavy Firing";
        return $this->situation;
    }
}


$obj_super = new super();
$value = $obj_super->loadme();
echo $value['tilt'];

5 Comments

Thanks bro, I am just hoping this code does not make my page load slow :( :)
you seem overly concerned about the loading speed of your page - none of the discussed methods will have a measurable delay on your pages. Arguably - the above code will be the SLOWEST, as you are having to locate and assign a value to an associative array each time you access a function, then re-access that associative array when you want to print.
@JohnyBravo: Performance isn't an issue til it is. Worry about getting stuff working, and don't fret too much over the speed of your page til you can point to a particular problem with it.
Bug free, Security and faster performance is all my top 3 concern for which i am always coding bro. :) to me every second matters :)
well, again - this is a great way to cause bugs down the road, as your doing something pretty strange here, also - as I said before, this is likely to be the slowest method. And has no impact on security at all.
1

Perhaps you can design a class like this

<?php    
    class super {
        private $_tilt; 
        private $_straight;
        private $_round;
        private $_sleep;

        private $_hit;
        private $_bullet;
        private $_gun;

        public function loadme() {
            $this->_tilt = "I am tilted";
            $this->_straight = "I am Straight";
            $this->_round = "I am round";
            $this->_sleep = "I am Sleeping";
        }

        public function fireme() {
            $this->_hit = "I am hit";
            $this->_bullet = "I got bullet";
            $this->_gun = "Cover me! I am receiving Heavy Firing";
        }

        public function getTilt() {
            return $this->_tilt;
        }

        // Other getter functions
    }
?>

Then invoke the getter functions

<?php
    $oSuper = new super;
    $oSuper->loadme();
    echo $oSuper->getTilt();
?>

3 Comments

I don't want to use getter fucntion for each variable, it will make my page slow i guess. ?
www.google.com/search?q=website+optimisation+php+google+developer
I got it working via this phpfiddle.org/api/raw/xw7-smb . Hope I am not doing any wrong there.. If it is right can someone post this, so that i can accept as answer. I am not able to add my answer because of my less reputation
1

if you want to call it the exact way you are calling it you can return the object itself from each function. Try the following

class super {
    public $tilt = '';
    public $straight = '';
    public $round = '';
    public $sleep = '';
    public $hit = '';
    public $bullet = '';
    public $gun = '';
    public function loadme() {
        $this -> tilt = "I am tilted";
        $this -> straight = "I am Straight";
        $this -> round = "I am round";
        $this -> sleep = "I am Sleeping";
        return $this;
    }

    public function fireme() {
        $this -> hit = "I am hit";
        $this -> bullet = "I got bullet";
        $this -> gun = "Cover me! I am receiving Heavy Firing";
        return $this;
    }

}

$obj_super = new super();
echo $obj_super -> loadme() -> tilt;
echo $obj_super -> fireme() -> hit;

again as @TheNytangel suggested the following also will work with this class. if you want to suppress this behavior also you may need to redesign your functions as objects.

$obj_super = new super();
$obj_super -> loadme();
echo $obj_super -> tilt;
$obj_super -> fireme();
echo $obj_super  -> hit;

5 Comments

are you getting any error messages? I tested it before posting and it was working for me
NO Error with no value.. Blank my php version 5.4.9
I am using PHP 5.4.16 and the code i posted works for me! i don't think the PHP version matters as this is very basic class and works on all PHP version unless it is before OOP support. The output I get is I am tiltedI am hit
I got it working via this phpfiddle.org/api/raw/xw7-smb . Hope I am not doing any wrong there.. If it is right can someone post this, so that i can accept as answer. I am not able to add my answer because of my less reputation
if you are going to return the values in an array you don't need instance variables. check this fiddle phpfiddle.org/main/code/5qe-hn6
0

As per OP's code in fiddle with modification. I have removed the instance variables. New fiddle here http://phpfiddle.org/main/code/5qe-hn6

<?php    
class super {

    public function loadme() {
        $title=array();
        $title['tilt'] = "I am tilted";
        $title['straight'] = "I am Straight";
        $title['round'] = "I am round";
        $title['sleep'] = "I am Sleeping"; 
        return $title;
    }

    public function fireme() {
        $situation=array();
        $situation['hit'] = "I am hit";
        $situation['bullet'] = "I got bullet";
        $situation['gun'] = "Cover me! I am receiving Heavy Firing";
        return $situation;
    }
}
?>

<?php
    $obj_super = new super();
    $value = $obj_super->loadme();
    echo $value['tilt'];
?>

2 Comments

wow.. :) thank you so much sir. offtopic : are you really coding from last 24 year, where in India you are from, do you have any blog or so, i would like to visit them. :)
yes i started programming with 'Acorn Atom' and 'BBC Micro'. sry currently don't have my blog site up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.