Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • stdClass in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 487
    Comment on it

    "stdClass" is an void class of PHP. It can be as a act of the alternative of a associative array but works only on PHP 5 and more Any associative array. You thought it of a object as same in java or python.

    $info = array("question" => "school of", "answer" =&>100);
      $ic = (object) $info;
      print $ic->answer . "\n";
      print $ic->question . "\n";
    

    The result shown like this

    100
      school of
    

    If you want to see what that looks like inside:

    $cando = get_object&_vars($ic);
      var_dump($cando);
    

    which gives me:

    array(2) {
        ["question"]=> string(4) "school of"
        ["answer"]=>
        int(100)
      }
    

    Now the 'stdClass' comes with an extra feature like you can scale and fir more character and behaviours in it.

     class thing extends stdClass {
        public function assign($key , $value = false) {
          $this->$key = $value;
        }
        public function getpairs() {
          $pointer = get_object_vars($this);
          return $pointer;
        }
      }
    

    And let's now make some used of that:

    $story = new thing();
      $story->draw("in","Canvas");
      $story->ended = "morning";
    
      print $story-> . "\n";
      print $story->draw . "\n";
      print "--\n";
    
      $about = $story->getpairs();
      foreach (array&_keys($about) as $k) {
      print ("$k - $about[$k]\n");
      }
    

    The output of code will be like this

      Canvas
     morning
      -----
      in - Canvas
      ended - morning
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: