/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Class Reference

Does php have a built in function to reference a class that “$this” is an instance of?

In python __class__ is a built-in attribute of every class instance (of every class). It is a reference to the class that self is an instance of. You can use this to affect the class itself, not just the newly created instance.

This is something that I would find very useful in php.
If I didn’t explain it clearly, you can look here (at example 5.18):
[url]http://diveintopython.org/object_oriented_framework/class_attributes.html[/url]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 17.2010 — [url=http://www.php.net/manual/en/language.constants.predefined.php]Magic Constants[/url], where you'll see "__class__" is one.
Copy linkTweet thisAlerts:
@eval_BadCode_authorNov 22.2010 — Can you please duplicate the example provided by the url?

I've been trying to get this to work and the best solutions I can come up with are just evaluating junk.

Am I the only one who does this?:
[code=php]<?php

class A { }
class B { }
$x = 'B';
if(1){
$badcode =<<<EOF
class C extends $x {
public static function help(){
print "help ";
}
}
EOF;

eval( $badcode );
}
C::help();
$o = new C;
if ( $o instanceof $x )
{
print "me! ";
}

?>[/code]
Copy linkTweet thisAlerts:
@NogDogNov 22.2010 — Maybe what you're looking for is static class variables?
[code=php]
<?php

class A
{
public static $var = 1;
public function setVar($value)
{
self::$var = (int) $value;
}
public function getVar()
{
return self::$var;
}
}

$foo = new A();
$bar = new A();
echo A::$var . "<br />n"; // 1
echo $foo->getVar() . "<br />n"; // 1
echo $bar->getVar() . "<br />n"; // 1
$foo->setVar(5);
echo A::$var . "<br />n"; // 5
echo $foo->getVar() . "<br />n"; // 5
echo $bar->getVar() . "<br />n"; // 5
[/code]
Copy linkTweet thisAlerts:
@eval_BadCode_authorNov 22.2010 — What I'm looking to do is create objects. I just don't know how many. Maybe 1, maybe 200; it depends on whats in my database.

Then I want to know how many objects got created. I also want the objects to be able to interact with each other.

Maybe I can create an object that creates new objects?
Copy linkTweet thisAlerts:
@NogDogNov 22.2010 — Factory pattern, perhaps? But without really knowing what you actually want to accomplish, I'm still not sure. (E.g.: maybe the decorator pattern would be better, or some other pattern?) Only thing I'm pretty sure about is that eval() is probably not the best solution. ?
Copy linkTweet thisAlerts:
@eval_BadCode_authorNov 22.2010 — I'm trying to accomplish exactly this:

I want to display utility data (meter readings).

The data is kept in mysql. It's a relational model. I want to display it in a hierarchical model.

I always want to display every meter for a single route.

It would look like this in the hierarchical model.

[FONT="Fixedsys"]

=============== Request 1 ===============

-- (+) Route A

-- --- (+) ParkingLot A.1

-- --- ---(-) Meter A.1.1

-- --- ---(-) Meter A.1.2

-- --- ---(-) Meter A.1.3

-- --- ---(-) Meter A.1.4

-- --- (+) ParkingLot A.2 //<---- click here to hide Lot A.2 meters

-- --- ---(-) Meter A.2.1

-- --- ---(-) Meter A.2.2

-- --- ---(-) Meter A.2.3

-- --- ---(-) Meter A.2.4


=============== Request 2 ============

-- (+) Route A

-- --- (+) ParkingLot A.1

-- --- ---(-) Meter A.1.1

-- --- ---(-) Meter A.1.2

-- --- ---(-) Meter A.1.3 //<---- click here to look at Meter A.1.3

-- --- ---(-) Meter A.1.4

-- --- (-) ParkingLot A.2

=============== Request 3 ============

-- (+) Route A

-- --- (+) ParkingLot A.1

-- --- ---(-) Meter A.1.1

-- --- ---(-) Meter A.1.2

-- --- ---(+) Meter A.1.3

-- --- [-----------------------------------------]

-- --- [--- a graph or other applicable things --]

-- --- [-----------------------------------------]

-- --- ---(-) Meter A.1.4

-- --- (-) ParkingLot A.2

[/FONT]



I want the class Routes to contain instances of the class ParkingLot

I want the instances of class ParkingLot to contain instances of the class Meter.

I want the instances of class Meter to contain data that I care about.

I want to serialize an instance of a route across requests in a session.

A request would involve manipulating the instances of classes contained in the instance of class Route to show useful information. Like usage, (+/-)&#37; increase in usage since the last reading or over time, or display a graph.

Those + and - signs in the display above... a meter instance should know if it's supposed to be displayed or not.

Imagine Google letting you search through your search results. Then maybe undo a level of searching, then continue to narrow your results to 5 or 10.

I'm stuck at getting an instance of a class to contain a variable number of other instances of another class. I only need to create this instance once and then it can be used to navigate a database like a boss, or it can be recreated/reset and all of the display information is lost and you can start over.

It would also be nice if for any instance of ParkingLot it knew how many instances of Meter it contained.

Edit:

Maybe implement an ajax version where you can contain each instance in a block element, and a request can manipulate a serialized version of the instance of class route from the session in an ajax request. You could hover over a block element and click on it to get a menu of ways to manipulate it... I'm not saying that the entire database needs to be crunched into a session all at once.
Copy linkTweet thisAlerts:
@eval_BadCode_authorNov 22.2010 — Wow... that was stupid easy. Would have been a complete nightmare using eval()


This was all I really needed.

[code=php]
class display
{
final static public function block($mult_indent = 1,$source) {
echo "<div style="display:block;padding-left:".(15*$mult_indent).";" >".$source."</div>";
}
}

class NodeRoot {
public $routes = array();
private static $indent = 0;
public function __construct() {
$result = mysql_query("SELECT DISTINCT route FROM key_sets");
mysql_num_rows($result) ? $this->create_subnodes($result) : display::block(self::$indent,'No results for routes found in the database.');
}

private function create_subnodes($result) {
while ($r = mysql_fetch_array($result)) {
$this->routes[$r['route']] = new Route($r['route']);
}
}
}

class Route {
public $meter_groups = array();
private static $name;
private static $indent = 2;

public function __construct($name) {
self::$name = $name;
display::block(self::$indent,self::$name);
$result = mysql_query("SELECT DISTINCT bill_name FROM key_sets WHERE route='".self::$name."'");
mysql_num_rows($result) ? $this->create_subnodes($result) : display::block(self::$indent,'No results for meter groups found in the database.');
}
private function create_subnodes($result) {
while ($r = mysql_fetch_array($result)) {
$this->meter_groups[$r['bill_name']] = new meter_group($r['bill_name']);
}
}
}

//... and so on down to the single meter

[/code]


$root = new NodeRoot();

Here's how I'd reference the function 'details' for the first Meter.

$root->routes['Route A']->meter_groups['ParkingLot A.1']->meters['Meter A.1.1']->meter::details();
×

Success!

Help @eval_BadCode_ spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...