[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

reflection class to echo all the properties

Posted on 2014-04-28
4
Medium Priority
?
481 Views
Last Modified: 2014-04-30
<?php
require_once "fullshop.php";
class ReflectionUtil {
  static function getMethodSource( ReflectionMethod $method ) {
    $path = $method->getFileName();
    $lines = @file( $path );
    $from = $method->getStartLine();
    $to   = $method->getEndLine();
    $len  = $to-$from+1;
    return implode( array_slice( $lines, $from-1, $len+9 ));
  }
}

//$class = new ReflectionClass( 'CdProduct' );
//$method = $class->getMethod( 'getSummaryLine' );
$class = new ReflectionClass('shopProduct');
$method = $class->getMethod('__construct');
print ReflectionUtil::getMethodSource( $method );
$properties=$class->getProperties('__construct');
foreach ($properties as $property){
  echo '<br>property: '.$property;
}
?>

Open in new window



output

public function __construct( $title, $firstName, $mainName, $price ) { $this->title = $title; $this->producerFirstName = $firstName; $this->producerMainName = $mainName; $this->price = $price; } public function getProducerFirstName() { return $this->producerFirstName; } public function getProducerMainName() { return $this->producerMainName; }
Warning: ReflectionClass::getProperties() expects parameter 1 to be long, string given in C:\wamp\www\POPP-edition4-code\9781430260318_Chapter_05_Code\listing5.34.php on line 19

Warning: Invalid argument supplied for foreach() in C:\wamp\www\POPP-edition4-code\9781430260318_Chapter_05_Code\listing5.34.php on line 20

I want to echo all the properties of the method __construct in the shopProduct class
I want to echo all the properties  in the shopProduct class
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40029138
I want to echo all the properties of the method __construct in the shopProduct class
PHP OOP does not work that way.  "Properties" are the data elements of the object (not limited to any one method) and they are visible according to their definition as public, protected, or private.
http://php.net/manual/en/language.oop5.properties.php
http://php.net/manual/en/language.oop5.visibility.php

Within a method, there may be local variables.

<?php // demo/temp_rgb192.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28421888.html

Class Rgb192
{
    protected $myVar = 3;
    public function __construct($x)
    {
        $this->x = $x;
    }
    public function showMe()
    {
        $y = 5;
        return get_defined_vars();
    }
}

$obj = new RGB192('ABC');

// FINDS THE LOCAL VARIABLES INSIDE THE SHOWME() METHOD
var_dump($obj->showme());

// FINDS ALL OF THE PROPERTIES OF THE OBJECT - DOES NOT INCLUDE LOCAL VARS
var_dump($obj);

Open in new window

For a deeper understanding of how to produce meta-data about your classes, you may want to make a tour-de-force study of this, setting up code examples to demonstrate the various methods:
http://php.net/manual/en/intro.reflection.php
0
 

Author Comment

by:rgb192
ID: 40029874
So object is properties and methods
Reflection is class
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 40029975
Yes, but there is a lot to learn about Reflection.  This seems to be a good starting point:
http://www.php.net/manual/en/class.reflection.php#99616
0
 

Author Closing Comment

by:rgb192
ID: 40031904
thanks for reflection information
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question