PHP - Oop Access Private Variable In Inherited Class
Hi,
when i extend a class containing a private variable and then access the variable in the extended class it doesn't produce any error and works fine and when i access the variable using the object of the extended class it works even then, i think private variable should not be accessable in an extended class neither by object of that extended class here is the code <?php class textbox{ var $body_text, $body_text1; private $body_private="this is private value"; protected $body_protect="this is protected value"; function __construct($text_in, $text_out){ $this->body_text=$text_in; $this->body_text1=$text_out; } function display(){ echo "$this->body_text<br>"; echo "<i>"; echo "$this->body_text1</i><br>"; echo "$this->body_private<br/>"; echo "$this->body_protect<br/>"; } function test_new(){ echo "see if this private echo works"; } } $box=new textbox("any new value","any old value" ); $box->display(); $box->body_text="this is public thats why i can access it"; $box->body_text1="this is public thats why i can access it"; /*$box->body_private="this is private value changed"; //you will see we can't access the private and protected memebers from outside $box->body_protect="this is protected value changed"; */ $box->display(); $box->test_new(); class textboxbold extends textbox{ function __construct($text_on, $text_off, $text_get, $text_go) {$this->body_text=$text_on; $this->body_text1=$text_off; $this->body_protect=$text_get; $this->body_private=$text_go; } function display(){ echo "<b>$this->body_text</b><br>"; echo "<b>$this->body_text1</b><br>"; echo "<b>$this->body_protect</b><br>"; echo "<i>$this->body_private</i><br>"; } } $boxnew=new textboxbold("new Bold text", "text in bold","protected member","private"); $boxnew->display(); $boxnew->body_private="this is simple text taken from outside"; $boxnew->display(); $boxnew->test_new(); ?> please recommend, is it working as it should be Similar TutorialsI am trying to class type inheritance with Doctrine. Eventually, I will be defining things using XML, but all the examples are done using doctags. I found the following and it seems to work. <?php use Doctrine\ORM\Mapping as ORM; /** * Inventory entity * * @ORM\Entity(repositoryClass="Inventory") * @ORM\Table(name="inventory") * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="discriminator_column", type="string") * @ORM\DiscriminatorMap({"inventory" = "AbstractItem", "inventory_room" = "Room"}) */ abstract class AbstractItem { /** * @ORM\Id @ORM\Column(name="inventory_id") @ORM\GeneratedValue * @var int */ protected $inventory_id; /** * @ORM\Column(type="string") * @var string */ protected $category; /** * @ORM\Column(type="string") * @var string */ protected $part_number; } use Doctrine\ORM\Mapping as ORM; /** * Inventory Room entity * @ORM\Entity * @ORM\Table(name="inventory_room") */ class Room extends AbstractItem { /** * @ORM\Column(type="string") * @var string */ protected $room_type; /** * @ORM\Column(type="string") * @var string */ protected $rack_limit;
But then I convert it to XML or YAML, and I get a Entity class 'Room' used in the discriminator map of class 'AbstractItem' does not exist error when trying to build the classes. I've also tried changing the type to mappedSuperclass, but no success. Any ideas? Also, what is the purpose of the DiscriminatorColumn? Thanks
AbstractItem: type: entity table: inventory inheritanceType: JOINED discriminatorColumn: name: discriminator_column type: string discriminatorMap: inventory: AbstractItem inventory_room: Room repositoryClass: Inventory id: inventory_id: type: string fields: category: type: string part_number: type: string Room: type: entity table: inventory_room fields: room_type: type: string rack_limit: type: string
<?xml version="1.0" encoding="utf-8"?> <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> <entity repository-class="Inventory" name="AbstractItem" table="inventory" inheritance-type="JOINED"> <discriminator-column name="discriminator_column" type="string" length="255"/> <discriminator-map> <discriminator-mapping value="inventory" class="AbstractItem"/> <discriminator-mapping value="inventory_room" class="Room"/> </discriminator-map> <id name="inventory_id" type="string" column="inventory_id"> <generator strategy="IDENTITY"/> </id> <field name="category" type="string" column="category" precision="0" scale="0" nullable="false"/> <field name="part_number" type="string" column="part_number" precision="0" scale="0" nullable="false"/> </entity> </doctrine-mapping> <?xml version="1.0" encoding="utf-8"?> <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> <entity name="Room" table="inventory_room"> <id name="inventory_id" type="string" column="inventory_id"> <generator strategy="IDENTITY"/> </id> <field name="room_type" type="string" column="room_type" precision="0" scale="0" nullable="false"/> <field name="rack_limit" type="string" column="rack_limit" precision="0" scale="0" nullable="false"/> <field name="category" type="string" column="category" precision="0" scale="0" nullable="false"/> <field name="part_number" type="string" column="part_number" precision="0" scale="0" nullable="false"/> </entity> </doctrine-mapping>
Dear all , i am trying the following : i have a class named ACCOUNT with many properties in .some of these properties are array , it is like this : Code: [Select] class ACCOUNT { PRIVATE $DB_LINK; PRIVATE $COMP; PRIVATE $BRANCH; PRIVATE $CURRENCY; PRIVATE $GL; PRIVATE $CIF; PRIVATE $SL; PRIVATE $EXIST; PRIVATE $STATUS; private $ACCOUNT_NAME=ARRAY("LA"=>'',"LE"=>'',"SA"=>'',"SE"=>''); private $ACCOUNT_BALANCE =ARRAY('FC_YTD','CV_YTD','CV_BAL','YTD_BAL','BLOCKED_CV','BLOCKED_FC'); private $CY_NAME=ARRAY("LA"=>'',"LE"=>'',"SA"=>'',"SE"=>''); private $ACCOUNT_NAME_USR=ARRAY("LA"=>'',"LE"=>'',"SA"=>'',"SE"=>''); private $LEDGER_NAME= ARRAY("LA"=>'',"LE"=>''); i have created the following method to call any property [code] FUNCTION GET_SPECIFEC_ATT($ATT,$LANG) { $ATT=$ATT."['L$LANG']"; ECHO $this->$ATT; } but i am getting the below error : Notice: Undefined property: ACCOUNT::$BRANCH_NAME['LA'] in D:\wamp\www\EBANK\account.class on line 186 if i used this : Code: [Select] echo $this->BRANCH_NAME['LA']; it is working fine . and the method is working fine i can iam trying to call property which is NOT an array. Can you please help me in what iam doing wrong ? Thanks in advance Hi there people of the PHP forum, This functions intention is to output the same code 4 digit code twice before generating a new one, I can get it to do one fine, but I cannot for the life of me figure out how to get it to repeat twice before changing on the third call. private function KeyGen(){ $Numerical = range(0,9); $Alpha = range("A","Z"); $Keys = array_merge($Numerical, $Alpha); return $Keys[rand(0,35)].$Keys[rand(0,35)].$Keys[rand(0,35)].$Keys[rand(0,35)]; } I am needing it to do this so that each 'set' of 2 calls to the function (within the class) gives the same 4 digit code. At the moment I am just making one call & then assigning that single call to a variable and then referencing that var twice, this is not what I want to do, but it scratches the itch for now... Any idea's/other logic would be greatly appreciated. Cheers, Rw I have mysqli object in Database class base: [color=]database class:[/color] class Database { private $dbLink = null; public function __construct() { if (is_null($this->dbLink)) { // load db information to connect $init_array = parse_ini_file("../init.ini.inc", true); $this->dbLink = new mysqli($init_array['database']['host'], $init_array['database']['usr'], $init_array['database']['pwd'], $init_array['database']['db']); if (mysqli_connect_errno()) { $this->dbLink = null; } } } public function __destruct() { $this->dbLink->close(); } } Class derived is Articles where I use object dBLink in base (or parent) class and I can't access to mysqli methods (dbLink member of base class): Articles class: require_once ('./includes/db.inc'); class Articles extends Database{ private $id, .... .... $visible = null; public function __construct() { // Set date as 2009-07-08 07:35:00 $this->lastUpdDate = date('Y-m-d H:i:s'); $this->creationDate = date('Y-m-d H:i:s'); } // Setter .... .... // Getter .... .... public function getArticlesByPosition($numArticles) { if ($result = $this->dbLink->query('SELECT * FROM articles ORDER BY position LIMIT '.$numArticles)) { $i = 0; while ($ret = $result->fetch_array(MYSQLI_ASSOC)) { $arts[$i] = $ret; } $result->close(); return $arts; } } } In my front page php I use article class: include_once('./includes/articles.inc'); $articlesObj = new articles(); $articles = $articlesObj->getArticlesByPosition(1); var_dump($articles); [color=]Error that go out is follow[/color] Notice: Undefined property: Articles::$dbLink in articles.inc on line 89 Fatal error: Call to a member function query() on a non-object in articles.inc on line 89 If I remove constructor on derived class Articles result don't change Please help me HI everyone! So basically I have this class called Login and another class classed Reports. They both extend a main class called OOP. I'm trying to get classes now and in the future, when I add on, to access that class so that way I dont have to create a new object everytime I need to do that. Plus I know I dont want to rely on calling another class inside of one class. Here is an example The Super Class class OOP{ public function Login($pointer){ $Login->{$pointer}(); } public function Reports($pointer){ $Reports->{$pointer}() } } Login Class class Login extends OOP{ public function userLogin($user, $pass){ //Login code here //if error occurs, send it to Reports super::Reports(Error()); } } Reports Class class Reports extends OOP{ public function Error(){ //Send an error here } } Here is how I think I would call the class if a user was to login. $OOP = new OOP(); $OOP->Login(userLogin($user, $pass)); So now when I need to call any class I should be able to, correct? If you are confused about the top, then think of it this way: I am trying to create a class to where I can call or reference to ANY object now or in the future so I can add on and call that class from another class. Thank you for any help. I am having problem with calling class member from with the function in that class. <?php class board { function __construct () { $num_sheep_init=20; $num_lion_init=2; $board_value=array(0=>'', 1=>'', 2=>'', 3=>'', 4=>'', 5=>'', 6=>'', 7=>'', 8=>'', 9=>'', 10=>'', 11=>'', 12=>'', 13=>'', 14=>'', 15=>'', 16=>'', 17=>'', 18=>'', 19=>'', 20=>'', 21=>'', 22=>'', 23=>'', 24=>''); $random_array_value=array (0,1,2,3,4,5,7,9,10,11,13,14,15,17,19,20,21,22,23,24); $selected_index=array(); $randd=array(); $countt=0; } function init_board () { $this->board_value['6']="S"; Above is part of the code where i am having problem. It says that it can not access empty property //$this->board_value['6']="S"; I have these two classes: Code: [Select] <?php class A { private $id; public function __construct() { $this->id = '11111'; } public function getProperty($prop) { return $this->$prop; } }?> Code: [Select] <?php class B extends A { private $message = "Nope, can't see it"; public function __construct() { parent::__construct(); $this->message = "Yep, I can see it"; } }?> I tried this, but it just outputs the id (the first call) Code: [Select] $class = new B; echo $class->getProperty('id'); echo $class->getProperty('message'); Why is that? I thought I could use a parent method on a child property.. *EDIT* Using a public or protected visibility on the message property gets me the output I expect...how come? Hey, this is my first time using this forum, so I apologize if I miss something and I'd like to thank you in advance for your help. I inherited some code from a different php programmer and it's not quite working right. The basics: We have a form for users to fill out where they select a Township from a drop down menu, then based on the township they choose there is a 2nd drop down populated with Range values. I can get the Township drop down to populate from the mysql database, but once you select the Township, the 2nd drop down does not populate as expected. This worked on the old web server (which I regrettably no longer have access to), but it's not working on the new web server. I hope that the problem is just a version issue where some syntax changed, but I'm not sure. Here's the function: Code: [Select] <?php echo "<form method='post' name='tsform' id='tsform' action='".$_SERVER['PHP_SELF']."'><input type=hidden name='page' id='page' value='viewfntown'>"; echo "<table><tr VALIGN=baseline><td></td>"; echo "<td>Township: <select name='ts' id='ts'><option value=''></option>"; $sql="SELECT distinct(township) from `linksurv_field_notes` WHERE `township`!='' order by `township`;"; $result=mysql_query($sql) or die("ts sel:".mysql_error()); $NumResults=mysql_num_rows($result); for ($q=1;$q<=$NumResults;$q++) { $row=mysql_fetch_row($result); $township=$row[0]; echo "<option value='".$township."'>".$township."</option>"; } echo "</select></td>"; echo "<td>Range: <select name='rg' id='rg'><option value=''></option>"; $sql="SELECT distinct(range) from `linksurv_field_notes` WHERE `range`!='' order by `range`;"; $result=mysql_query($sql) or die("rg sel:".mysql_error()); $NumResults=mysql_num_rows($result); for ($q=1;$q<=$NumResults;$q++) { $row=mysql_fetch_row($result); $range=$row[0]; echo "<option value='".$range."'>".$range."</option>"; } echo "</select></td>"; echo "<td><button type=button onClick=\"return submitCorner('NA','NA');\">Show All Corners</button></td>"; echo "</tr>"; echo "</table>"; ?> Also, here is the error I see in Firebug: Code: [Select] rg sel:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'range) from `linksurv_field_notes` WHERE `range`!='' order by `range`' at line 1 I've been stumped on this for a few weeks and any help you can give is appreciated. If I left some details out, just ask. Thanks. I have a class that creates a grid from an xml feed. The class file is attached. The grid displays on a page which is defined within the class as follows: Code: [Select] private $results_page = '/foobar.php'; I'm including the same class in multiple folders (the folders are addon domains) so I can have just one copy of the class so it is easy to modify. The problem is that I need to add a variable containing the folder name to the $results_page path, so that when the class executes within each folder, the $results_page is 'FOLDER//foobar.php' I have a file with defines in each folder, and I have tried including that file in the class and using the defined var in the construct, but I can't get it to work. What am I missing? So, I was given the task of editing a website. It was done in PHP for the most part.
If you visit the page http://cre-finance.com/?Contact-Us, I'm attempting to edit the contact info at the bottom but I have no idea where to find it in the website files. If you the view the source, it's right there like it's taunting me but none of the php files have that info in it. There is a contactus.php (I attached the code for it) but all it has is the code for the form at the bottom of the Contact Us page.
I've noticed references to "?p=Contact-Us" in the source, and also in the sitemap.xml file but I have no idea what that really means because there are no other "contactus" pages other than the one I mentioned above. I guess I'm not really looking for help with actual coding but perhaps to be pointed in the right direction to edit that information.
HELP!
Attached Files
contact_us.php 3.55KB
0 downloads I have a method that is returning a funny variable, it should be returning what is below and when I echo the return from inside the method it is right. Code: [Select] Software/Section but when I goto echo the from outside the class I get the following. It adds a back slash Code: [Select] Software//Section this is the method code and it works fine when I echo the returnValue the value is right Code: [Select] // Creates a dymamic case statement for making a the choice // inside and array foreach (scandir($dir) as $folderItem) { if ($folderItem != "." AND $folderItem != "..") { if (is_dir($dir.$folderItem.DIRECTORY_SEPARATOR)) { $folderItem = $folderItem ; // this allows the the dir to be placed if statements below $folderContents = $this->showPage( $dir.$folderItem ,$getPageGlobal , $subdir); $subdir = ""; // this resets so that ithe value does not get added to main links; } else { // put into if into array if($getPageGlobal== $folderItem) { $pageReturn = $dir . $folderItem; } } } } echo $pageReturn.'<br>'; return $pageReturn; This is the way I am echoing the class. This is when it decides to add the back slash Code: [Select] echo $dynamicMenu->getShowPage(); I am working on a site that was made a while ago and on a custom page I am making I am needing to call up variables and my brain is too fried to think up the solution for this. The standing code is this: class site{ var $name = 'Site'; var $data = 'on'; { What I need to do is callup the variables, such as $name, as simple as possible. How would I do this? Using this locally works fine: $html = $name::add_content($name, $html); but i get the double colon error whe using it live. $name is a name of a valid class. What could throw this error when it works locally? If i declare protected variable in a class how can i access those variable in other class inheriting that class? Can anyone post example code? Code: [Select] class replayer extends users { --SNIP-- public $islive = false; --SNIP-- function chat($id, $nick, $chat) { --SNIP-- <----------- } } There's my code, I want to take the variable $islive from the above class and use it in the chat() function. How would I do it? I've tried making it a global inside of the chat() function. Hi, I have following code : Code: [Select] <?php class CategoryWork { public $form = ""; public $error = ""; public $add_form = "<br /><br /><p><strong>Add New Category</strong></p><br /><form id=\"form1\" name=\"form1\" method=\"post\" action=\"category.php?action=add\"> <table width=\"550\" height=\"170\" border=\"0\"> <tr> <td width=\"153\">Name :</td> <td colspan=\"2\"><label for=\"cat_name\"></label> <input name=\"cat_name\" type=\"text\" id=\"cat_name\" size=\"50\" maxlength=\"50\" /></td> </tr> <tr> <td>Slug :</td> <td colspan=\"2\"><label for=\"cat_slug\"></label> <input name=\"cat_slug\" type=\"text\" id=\"cat_slug\" size=\"50\" maxlength=\"50\" /></td> </tr> <tr> <td>Description</td> <td colspan=\"2\"><label for=\"cat_desc\"></label> <textarea name=\"cat_desc\" id=\"cat_desc\" cols=\"48\" rows=\"10\"></textarea></td> </tr> <tr> <td> </td> <td width=\"97\"><input type=\"submit\" name=\"button\" id=\"button\" value=\"Submit\" /></td> <td width=\"286\"><input type=\"reset\" name=\"button2\" id=\"button2\" value=\"Reset\" /></td> </tr> <tr> <td> </td> <td colspan=\"2\">error</td> </tr> </table> </form>"; } ?> At the line of <td colspan=\"2\">error</td> I want to insert $error, hence I made the line as <td colspan=\"2\">".$error."</td> and also tried <td colspan=\"2\">".$this->error."</td> After executing with this, php says syntax error at the line of public $add_form I am going to declare an error at the $error When I do <td colspan=\"2\">$error</td> it says : Parse error: syntax error, unexpected '"' at line of public $add_form When I do <td colspan=\"2\">".$error."</td> it says : Parse error: syntax error, unexpected '.', expecting ',' or ';' at the above line i.e. of <td> I am damn confused, I googled but nothing found any helpful material. Anybody please help me.. Thanks I have this as my autoloader. All the classes are based off of Class O. I wanted to be able to do something like $dog = new Dog(); even if I hadn't yet created Dog.class.php and made it extend O. (I realize there are plenty of reasons this is a bad idea, this is a temporary part of the project, I am just trying to get a few things set up and thought it would work - now I'm probably going to spend more time trying to do this than it would have saved, but I'm curious. ) ( ! ) Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING in C:\wamp\www\project\index.php on line 22 (line 22 is Class $class_name extends O{ Code: [Select] <?php function autoload($class_name) { if(file_exists($class_name.'.class.php')){ require_once($class_name.'.class.php'); }else{ Class $class_name extends O{ } } } ?> Is it possible to do what I'm trying to do? Hello I want to be able to change the class depending on if a variable is 1 or 0. <html> <p class="note-general"> This is the general </p> <p class="note-warning"> And this is the warning </p> </html> I want the variable $adult (that can be 1 or 0) to set if the class should be a note-general or note-warning. Thank you for support Currently, I'm trying to write a contact form, and perhaps I'm overcomplicating it, but I have a public variable called $EquationAnswer that stores the answer to the Captcha method (randomised maths question) and whenever the page reloads, it creates a new instance of the Contact class, which then erases the value that has been stored in that variable.
What I'm trying to achieve is keeping the value stored even after a new instantiation of the class. I remember from a class at University that static was a way to do this, but this was in C#.
<?php class Contact { public $EquationAnswer; public function CaptchaCreation() { $SignArray = array('+', '×', '-'); $NumberOne = rand(1, 9); $NumberTwo = rand(1, 4); if($NumberOne < $NumberTwo) $SignIndex = rand(0, 1); else $SignIndex = rand(0, 2); $Sign = $SignArray[$SignIndex]; $EquationString = $NumberOne." ".$Sign." ".$NumberTwo; switch($Sign) { case "+": $Answer = $NumberOne + $NumberTwo; break; case "-": $Answer = $NumberOne - $NumberTwo; break; case "×": $Answer = $NumberOne * $NumberTwo; break; } $this->EquationAnswer = $Answer; return $EquationString; } public function EchoContactForm() { $CaptchaMessage = $this->CaptchaCreation(); echo "<form method='POST' action=''>\n\n"; echo "\t\t\t\t\t<table>\n\n"; echo "\t\t\t\t\t\t<tr><td>Name: </td><td><input type='text' name='name' /></td></tr>\n"; echo "\t\t\t\t\t\t<tr><td>Email: </td><td><input type='text' name='email' /></td></tr>\n"; echo "\t\t\t\t\t\t<tr><td>Subject: </td><td><input type='text' name='subject' /></td></tr>\n"; echo "\t\t\t\t\t\t<tr><td>Message: </td><td><textarea rows=5 name='message'></textarea></td></tr>\n"; echo "\t\t\t\t\t\t<tr><td>What is: ".$CaptchaMessage."?</td><td><input type='text' name='captcha' /></td><td></td></tr>\n"; echo "\t\t\t\t\t\t<tr><td colspan=2 style='text-align: right'><input type='submit' name='submit' value='Send' /></td></tr>\n\n"; echo "\t\t\t\t\t</table>\n\n"; echo "\t\t\t\t</form>\n"; } public function ContactFormValidation($Name, $Email, $Subject, $Message, $CaptchaInput, $CaptchaAnswer) { echo $CaptchaAnswer; $ErrorsArray = array(); if(strlen($Name) == 0) array_push($ErrorsArray, "You must provide a name."); if(strlen($Email) == 0) array_push($ErrorsArray, "You must provide an email."); if(strlen($Message) == 0) array_push($ErrorsArray, "You must provide a message."); if(strlen($CaptchaInput) == 0) array_push($ErrorsArray, "You must provide an answer to the maths question."); if($CaptchaInput != $CaptchaAnswer) array_push($ErrorsArray, "Incorrect captcha answer."); if(count($ErrorsArray) > 0) { echo "There are ".count($ErrorsArray)." Errors: <br />"; foreach($ErrorsArray as $E) echo $E."<br />"; } else { return true; } } } ?>^ That's the Contact class, and the main function that I want to focus on is the last one, "ContactFormValidation()". From the contact form page (which is within a template), I want it to send the EquationAnswer variable to that function so that it can compare what the user has input. <?php //Instantiate the contact class include './inc/PHP/classes/ContactClass.php'; $ContactClass = new Contact; if(!isset($_POST['submit'])) { $ContactClass->EchoContactForm(); } else { $NameInput = $_POST['name']; $EmailInput = $_POST['email']; $SubjectInput = $_POST['subject']; $MessageInput = $_POST['message']; $CaptchaInput = $_POST['captcha']; $CaptchaAnswer = $ContactClass->EquationAnswer; //Tried echoing out what is in the variable but it produces nothing because the contact class is a new instance. //echo $CaptchaAnswer; if($ContactClass->ContactFormValidation($NameInput, $EmailInput, $SubjectInput, $MessageInput, $CaptchaInput, $CaptchaAnswer) == true) { //Mail function } } ?>Any help would be appreciated. Cheers. |