Asked  7 Months ago    Answers:  5   Viewed   42 times

i have 3 pages one for connect the DB

class Db{

private $dbUserName ="root";
private $dbName = "oop";
private $dbPas = "";
private $dbHost = "127.0.01";

public function __construct($dbUserName, $dbName, $dbPas, $dbHost)
{
    $this->dbUserName = $dbUserName;
    $this->dbName = $dbName;
    $this->dbPas = $dbPas;
    $this->dbHost = $dbHost;

    $con = new PDO("mysql:host= $dbHost; $dbUserName,$dbName,$dbPas");
    return $con ;
}

the 2 is

class User extends Db{
protected function getAllUser(){

    $sql = "SELECT * FROM user";
    $result = $this->connect()->query($sql);

    $count = $result->rowCount();
    // check if there'r date in the db
    if($count > 0){
        while($row = $result->fetchAll() ){
            $data[] = $row;
        }
        return $data;
    }

}

}

the 3 is

class ViewUser extends User{

public function ViewAllUser (){

    $datas = $this->getAllUser();

    foreach($datas as $data){
        //echo the db rows
        echo $data['uid']."</br>";
        echo $data['pas']."</br>";
    }

}

}

when I try to run them on another page:

<?php
    include 'Db.php';
    include 'User.php';
    include 'ViewUser.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Oop</title>
</head>
<body>

     <?php
        $users = new ViewUser();
        $users->ViewAllUser();
     ?>

</body>
</html>

i get a error:

Uncaught ArgumentCountError: Too few arguments to function Db::__construct(), 0 passed in /Users/mohamedelmasry/Documents/websites/oop/test.php on line 18 and exactly 4 expected in /Users/mohamedelmasry/Documents/websites/oop/Db.php:18 Stack trace: #0 /Users/mohamedelmasry/Documents/websites/oop/test.php(18): Db->__construct() #1 /Users/mohamedelmasry/.composer/vendor/laravel/valet/server.php(128): require('/Users/mohamede...') #2 {main} thrown in /Users/mohamedelmasr

 Answers

56

Your base class constructor expects 4 arguments:

class Db{

...

    public function __construct($dbUserName, $dbName, $dbPas, $dbHost)
    {
        ...
    }

So you can not create the child class without parameters. You need to either do:

$users = new ViewUser('root', 'dbname', 'pass', 'host');

Or put defaults into constructor declaration:

public function __construct($dbUserName='root', $dbName='db', $dbPas='pass', $dbHost='host')
{
    ...
}

But in general, the inheritance is misused here. You'd better have a separate Db class to manage DB and use it as a component inside your other classes (use composition instead of inheritance).

Wednesday, March 31, 2021
 
Lorav
answered 7 Months ago
77

First of all, __get, __set, etc. are defined public and you cannot have them otherwise. Magic methods should be used wisely as it takes about three times as long to make a call to a magic method than simply calling a class method.

class A {
   public function __get($name) { ... }

   public function __getValue() { ... }     // <== is faster

}

Usually (normally, preferably), you will have your class members private or protected (never public) and have accessors and mutators to encapsulate them. Those accessors and mutator may be of any visibility, depending on what the user can do with the members. You may also have immutable classes by declaring only accessors for your members, which are initialized in the constructor only.

So, your sample class should read

class classWithReadOnlyVar {
   private $readOnlyVar;

   public function getReadonlyVar() {
     return $this->readOnlyVar;
   }

}

and should not use the magic methods.

There may be many reasons to avoid using magic methods at all :

  1. they break code completion
  2. they are slower at run-time
  3. they make refactoring and maintenance a bit (lot) harder/complicated
  4. you can't have a protected magic method
  5. etc.

Class members

Their visibility should be private or protected, it all depends if you want them accessible by inheritence. They should never be public as this breaks the OO paradigm.

Example of a protected member:

class A {
    protected $_name = 'A';

    public function getName() { return $this->_name; }
}

class B {
    protected $_name = 'B';   // getName() will not return 'B'
}

(without $_name being protected, this would not be possible, and no need to redefine the accessor)

Accessors

They should be protected or public. Having a private accessor makes no sense; a class should access it's member directly. If the member needs processing, the class will know regardless when to call the accessor or not.

Mutators

They should be protected or public. As for the accessors, it makes no sense to have a private mutator... unless a very rare case when processing needs to be done internally. If a class member has no accessor, it should not have a mutator. It also makes no sense to have a mean to set a value without being able to get the value back somehow.

Wednesday, March 31, 2021
 
Sanguine
answered 7 Months ago
22

As you are calling function sendemail() from route. It's passing only one parameter to it like:

sendemail($request);

Also $total variable not being used in function sendemail() at all. So remove it or make it optional like:

public function sendemail(Request $request, $total = "") {
    $invoiceNo = $request->input('invoiceNo');
    $fname = $request->input('fname');
    $sendemail = $request->input('email');

    $data = [];
    $data['invoiceNo'] = $invoiceNo;
    $data['fname'] = $fname;
    $data['total'] = empty($total) ? 0 : $total;
    $data['sendemail'] = $sendemail;

    Mail::send(['html' => 'mail'], $data, function ($message) use ($data) {
        $message->to($data["sendemail"], 'TicketBooker')->subject
                ('CheapEfares Order Invoice');
        $message->from('kistlakall@gmail.com', 'CheapEfares');
    });
    return Redirect::back();
}

Also you can pass total variable through routes as well.

Route::get('sendemail/{total}','InvoicesController@sendemail')->name('sendemail');
Friday, May 28, 2021
 
jsuggs
answered 5 Months ago
70

I think the correct syntax for referencing ranges of cells in another sheet is:

List!$A$1:$A$10

So you should try:

$objValidation->setFormula1('List!$A$1:$A$10'); // tested it, worked for me

Got the idea from http://phpexcel.codeplex.com/discussions/320393:

->setFormula1("Worksheet!A1:{$endCell}1");// work....

Although this guy had another problem with using named ranges.

Background: I think with:

$objValidation->setFormula1('"$List.$A$1:$A$10"');

you're explicity using the given string between the quotation marks as the list value as explained here: here (where you probably got this snippet in the first place) or here. But since you don't want to use fixed list items but dynamically referred ones, you should omit the double quotation marks.

Saturday, May 29, 2021
 
Ula
answered 5 Months ago
Ula
23

Sometimes it happen that AdminCategory::deletecategory($parentId) is called without a parameter but prototype do not have a default value for it and therefore exception is thrown. Since you get data from a post request and there is always a possibility that a category do not have a parent you can refactor your method to looks like:

function deletecategory($parentId = null)
{
    $ids = $_POST['id'];
    $this->model->deletecategory($ids);
    if (null !== $parentId) {
        header('location:'.URL.'admincategory/showchildren/'.$parentId);
    }
    // PUT MORE OF YOUR LOGIC HERE, I DO NOT KNOW WHAT SHOULD HAPPEN
}

If you are using typing hints more appropriate would be to make method looks like

 function deletecategory(string $parentId = ''): void //void is for php7.1
 {
    $ids = $_POST['id'];
    $this->model->deletecategory($ids);
    if ('' !== $parentId) {
        header('location:'.URL.'admincategory/showchildren/'.$parentId);
    }
    // AGAIN LOGIC HERE
 }

If you REALLY expect that parentId MUST be passed then instead wrap method caller with try catch

if (method_exists($object, $this->method)) {
    try {
        call_user_func_array([$object, $this->method], $this->params);
    } catch (Exception $ex) {
        // HANDLE EXCEPTION HERE
    }
}
Saturday, May 29, 2021
 
kwichz
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :