Attempting to call a protected function from outside the class

OK I am new to PHP classes and trying to pass variables to protected functions in a class. How to do it?

CLASSES.PHP

<?php include($_SERVER['DOCUMENT_ROOT']."/includes/con.php"); class gindex { protected function rdev($a,$b,$c,$d){ $d = base64_encode($d); mysql_query("INSERT INTO mem(first_name,last_name,email,password,type) VALUES(".$a.",".$b.",".$c.",".$d.",'developer')", $db); } ?> 

index.php

 <?php include($_SERVER['DOCUMENT_ROOT']."/includes/con.php"); if(isset($_POST['developerbtn'])){ $fname = $_REQUEST['fname']; $lname = $_REQUEST['lname']; $email = $_REQUEST['email']; $password = $_REQUEST['password']; $Cgindex = new gindex(); $Cgindex->rdev($fname,$lname,$email,$password); } ?> 
0
source share
7 answers

You cannot do this because the method is set to protected , you must set it to public in order to be able to access it . Otherwise, you can call it only from the same class or from children of this class.

May I also suggest starting with your uppercase name: class Gindex . You can also improve the name of the class because gindex doesn't say anything about what it does. The same goes for your method name. And your parameter names are also terrible. Name things correctly so that people (including) themselves know exactly what the variable / class or method contains when they (re) view your code.

You also use the $db variable, which is nowhere in the scope of the class .

Also, please do not use mysql_* functions for new code. They are no longer supported, and the community has begun the process. See the red box ? Instead, you should learn about prepared instructions and use PDO or MySQLi . If you can’t decide, this article will help you choose. If you want to find out, here is a good PDO tutorial .

It will also eliminate the nasty SQL Injection that you have in your code. For more information on how to fix this using PDO or mysqli, see this question .

It also looks like you are using the website as a password when pasting data into the database. If you are really going to store a database, I also suggest that you read password hashing for security.

+4
source

I suggest you read the visibility docs again.

Declared protected members can only be accessed within the class itself and by inherited and parent classes.

$Cgindex->rdev(... calls the global context, not in the permitted context of the class.

+3
source

You cannot, the purpose of a protected function should only be called from within a class or inherited class

so that your function is public if you want to call it the same as you:

 public function rdev($a,$b,$c,$d){ 
+1
source

You need to read about OOP in general.
Protected function ... protected from using the class. Funds. Only code inside class methods or child classes can call a protected method. In addition, all child classes of the class that contains the protected method inherit this method.

+1
source

This should help:

Public - Any user of the class can get access to a public variable or method.

Protected - A protected variable or method may not be accessible to users of the class, but may be available inside a subclass that inherits from the class.

Private - A private variable or method can only be accessed within the class in which it is defined. This means that a private variable or method cannot be called from a child that extends the class.

+1
source

Protected elements are accessible only from the same class that were defined, and their subclasses are not externally.

0
source

 function __construct(){ $this->rdev(); } 

Initialize in Construct ..

0
source

Source: https://habr.com/ru/post/947575/


All Articles